还是上一篇的例子
存
// // main.m // sample005 // // Created by echoliu on 13-1-25. // Copyright (c) 2013年 echoliu. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSDictionary *glossary=[NSDictionary dictionaryWithObjectsAndKeys:@"a class",@"a", @"b class",@"b", @"c class",@"c",nil]; if ([NSKeyedArchiver archiveRootObject:glossary toFile: @"lossay.archiver"]==NO) { NSLog(@"save to file failed"); } } return 0; }
取
// // main.m // sample005 // // Created by echoliu on 13-1-25. // Copyright (c) 2013年 echoliu. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... //NSLog(@"Hello, World!"); /* NSDictionary *glossary=[NSDictionary dictionaryWithObjectsAndKeys:@"a class",@"a", @"b class",@"b", @"c class",@"c",nil]; if([glossary writeToFile:@"glossary" atomically:YES]==NO) NSLog(@"save to file failed"); */ /* NSDictionary *glossary ; glossary=[NSDictionary dictionaryWithContentsOfFile:@"glossary"]; for (NSString *str in glossary){ NSLog(@"%@ %@ ",str,[glossary objectForKey:str]); } */ /* NSDictionary *glossary=[NSDictionary dictionaryWithObjectsAndKeys:@"a class",@"a", @"b class",@"b", @"c class",@"c",nil]; if ([NSKeyedArchiver archiveRootObject:glossary toFile: @"lossay.archiver"]==NO) { NSLog(@"save to file failed"); } */ NSDictionary *glossary ; glossary=[NSKeyedUnarchiver unarchiveObjectWithFile:@"lossay.archiver"]; for (NSString *str in glossary){ NSLog(@"%@ %@ ",str,[glossary objectForKey:str]); } } return 0; }
实例来源教材