繁体   English   中英

使用iOS将数据保存到plist

[英]Saving data into plist with iOS

我正在尝试将数据添加到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];

在使用它们之前的某个地方。

关于警告,请使用编译器建议的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM