Ios 将标量写入文件
我有以下对象的数组:Ios 将标量写入文件,ios,ios5,nsarray,plist,Ios,Ios5,Nsarray,Plist,我有以下对象的数组: @interface SACameraLocation : NSObject { NSInteger locId; NSString *name; CLLocationCoordinate2D coordinate; NSInteger speed; NSInteger shootingMode; NSInteger side; } 然后我尝试将数组写入plist文件: if([arr writeToFile:path at
@interface SACameraLocation : NSObject
{
NSInteger locId;
NSString *name;
CLLocationCoordinate2D coordinate;
NSInteger speed;
NSInteger shootingMode;
NSInteger side;
}
然后我尝试将数组写入plist文件:
if([arr writeToFile:path atomically:YES])
NSLog(@"Write OK");
else
NSLog(@"Write Fail");
我想这是因为标量对象的缘故。将此类对象序列化/反序列化到文件的最简单方法是什么?请阅读
NSArray writeToFile:atomically:
:
如果数组的内容都是属性列表对象(NSString、NSData、NSArray或NSDictionary对象),则使用此方法编写的文件可以使用类方法arrayWithContentsOfFile:或实例方法initWithContentsOfFile:初始化新数组。此方法在写入文件之前递归验证所有包含的对象是否为属性列表对象,如果所有对象都不是属性列表对象,则返回否,因为生成的文件将不是有效的属性列表
不能写入自定义对象数组。这就是它失败的原因。您可以让您的类符合NSCoding
协议,并实现两个必需的方法。然后,您可以使用NSKeyedArchiver
将对象转换为NSData
。或者,您可以使用archiver一次对整个数组进行编码,然后写出一个NSData
对象
另一种选择是将对象转换为NSDictionary
,并写出这些字典的数组