2011年5月21日 星期六

Foundation Framework-歸檔 CH19-3,4 NSKeyedArchiver & NSKeyedUnarchiver

// 寫入檔案

// main.m

// CH19-3 NSKeyArchiver

//

// Created by jason on 2011/5/21.

// Copyright 2011 __MyCompanyName__. All rights reserved.

//


//#import

#import

#import

#import

#import

#import


int main(int argc, char *argv[])

{

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];

NSDictionary *glossary= [ NSDictionary dictionaryWithObjectsAndKeys: @"Apple Inc. is an American multinational corporation that designs and markets consumer electronics, computer software, and personal computers.", @"Apple Inc.",

@"Steven Paul ""Steve"" Jobs (born February 24, 1955) is an American business magnate and inventor. He is the co-founder and chief executive officer of Apple Inc.", @"Steve Jobs",

@"The Macintosh, or Mac, is a series of several lines of personal computers designed, developed, and marketed by Apple Inc.",@"Macintosh",nil];

[NSKeyedArchiver archiveRootObject:glossary toFile:@"glossary.archive"]; // 寫入檔案 glossary.archive中

[pool release];

return 0;

// return NSApplicationMain(argc, (const char **) argv);

}

*******************************************************

//讀取檔案

// main.m

// CH19-4 NSKeyedUnarchiver

//

// Created by jason on 2011/5/21.

// Copyright 2011 __MyCompanyName__. All rights reserved.

//


//#import

#import

#import

#import

#import

#import

#import


int main(int argc, char *argv[])

{

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];

NSDictionary *glossary;

glossary = [NSKeyedUnarchiver unarchiveObjectWithFile:@"glossary.archive"];

for(NSString *key in glossary)

NSLog(@"%@: %@", key, [ glossary objectForKey:key]);//讀取檔案

[pool drain];

return 0;

// return NSApplicationMain(argc, (const char **) argv);

}


沒有留言: