plist 文件不能存储一些 普通对象。
如:自己创建一个新的继承制NSObject的类(如A类)。在这个类中写两个属性。
因为A类没有 writeToFile这个方法,所以不能够写入到plist文件中。
这个时候可能会想,是不是把这个类的数据放到字典或者数组中。因为字典或者数组有这个 writeToFile 方法。
结果是:不能存储。
注:只要是普通对象,就不能调 writeToFile 方法。
- (void)saveArray { // 1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 2.document路径 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.新建数据 // MJPerson *p = [[MJPerson alloc] init]; // p.name = @"rose"; NSArray *data = @[@"jack", @10, @"ffdsf"]; NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; [data writeToFile:filepath atomically:YES]; } - (IBAction)read { // 1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 2.document路径 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.文件路径 NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; // 4.读取数据 NSArray *data = [NSArray arrayWithContentsOfFile:filepath]; NSLog(@"%@", data); }