标签:des style blog color io ar for 文件 数据
将数据以一定的格式写到文件,等到需要用的时候直接按照原来的形式读出来,这种方式叫做归档
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil]; //将字典写到文件夹里面 [dict writeToFile:@"/Users/fengze/Desktop/test1" atomically:YES]; //将文件读出来,直接生成一个字典 // NSDictionary dict2 = [[NSDictionary alloc] initWithContentsOfFile:@"/Users/fengze/Desktop/test1"]; NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil]; NSMutableData *data = [[NSMutableData alloc] init]; //当字典和数组想同时写入一个文件的时候 //创建一个归档管理员 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; //将数据打包 [archiver encodeObject:array forKey:@"array"]; [archiver encodeObject:dict forKey:@"dict"]; //完成打包 [archiver finishEncoding]; [data writeToFile:@"/Users/fengze/Desktop/test1" atomically:YES]; //反归档管理员 NSKeyedUnarchiver *unrchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //将打包的数组读出来 NSArray *array2 = [unrchiver decodeObjectForKey:@"array"]; //将打包的字典读出来 NSDictionary *dict2 = [unrchiver decodeObjectForKey:@"dict"]; } return 0; }
标签:des style blog color io ar for 文件 数据
原文地址:http://www.cnblogs.com/jia694600474/p/4012378.html