- (IBAction)save:(id)sender { Person *p = [[Person alloc] init]; p.age = 18; p.name = @"jack"; // 获取cache NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; // 获取文件的全路径 NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"]; // 把自定义对象归档 [NSKeyedArchiver archiveRootObject:p toFile:filePath]; }- (IBAction)read:(id)sender { // 获取cache NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; // 获取文件的全路径 NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"]; // 解档 Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"%d,%@",p.age,p.name); }