NSString *path = @"/Users/beyondsoft/Desktop/file.plist";
NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"one", @"1", @"2", @"two", @"three", @"5", nil];
[dict writeToFile:path atomically:YES];
[dict release];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
NSLog(@"%@", dict);
[dict release];
NSArray *array = [[NSArray alloc]initWithObjects:@"one", @"two", @"4", @"6", nil];
[array writeToFile:path atomically:YES];
[array release];
// 写入文件 归档文件,不同格式读写。
NSString *path = @"/Users/beyondsoft/Desktop/file.plist";
NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:@"one", @"1", @"2", @"two", @"three", @"5", nil];
NSArray *array = [[NSArray alloc]initWithObjects:@"one", @"two", @"4", @"6", nil];
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:array forKey:@"array"];
[archiver encodeObject:dict forKey:@"dic"];
[archiver finishEncoding];
[data writeToFile:path atomically:YES];
[array release];
[dict release];
[data release];
[archiver release];
//读取文件
NSString *path = @"/Users/beyondsoft/Desktop/file.plist";
NSData *data1 = [[NSData alloc]initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data1];
NSArray *array1 = [unarchiver decodeObjectForKey:@"array"];
NSLog(@"%@", array1);
NSDictionary *dict = [unarchiver decodeObjectForKey:@"dic"];
NSLog(@"%@", dict);
[data1 release];
[unarchiver release];
对类进行归档 //对一个类进行归档,类要实现NScodeing协议,并且要实现相应方法encodeWithCode, initWithCoder;
Dog *dog = [[Dog alloc]init];
dog.ID = 20;
NSString *path = @"/Users/beyondsoft/Desktop/file.plist";
NSDat
//对一个类进行归档,类要实现NScodeing协议,并且要实现相应方法encodeWithCode, initWithCoder;
Dog *dog = [[Dog alloc]init];
dog.ID = 20;
NSString *path = @"/Users/beyondsoft/Desktop/file.txt";
NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:dog];
[data1 writeToFile:path atomically:YES];
[dog release];
// read
NSData *data2 = [NSData dataWithContentsOfFile:path];
Dog *dog2 =[NSKeyedUnarchiver unarchiveObjectWithData:data2];
NSLog(@"%ld, %ld", dog.ID, dog2.ID);
a *data1 = [NSKeyedArchiver archivedDataWithRootObject:dog]; [data1 writeToFile:path atomically:YES]; [dog release];