我正在尝试将数据添加到 plist 但我不能,让我们告诉你我在做什么:
看看我的 plist:
让我们看看我的代码,我创建了 2 个数组:
@property NSMutableArray *nameArr;
@property NSMutableArray *countryArr;
这是我保存数据的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
[self.nameArr addObject:self.theName.text];
[self.countryArr addObject:self.cellPhone.text];
NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.nameArr, self.countryArr, nil] forKeys:[NSArray arrayWithObjects: @"city", @"state", nil]];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
[plistData writeToFile:plistPath atomically:YES];
NSLog(@"Data Saved");
}
else
{
NSLog(@"Data not saved");
}
下图显示错误,应用程序终止但我不知道问题出在哪里。
最佳答案
可能声明了数组属性但从未初始化。
你必须添加
nameArr = [[NSMutableArray alloc] init];
countryArr = [[NSMutableArray alloc] init];
在使用它们之前的某个地方。
关于警告使用编译器建议的方法。
关于ios - 使用 iOS 将数据保存到 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39549403/