无法将数据保存到plist

时间:2017-04-27 04:23:53

标签: ios objective-c dataset plist

以下是我保存数据的方法:

self.doctorString = @"Doctor Bob";

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"doctorprofile.plist"];

NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.doctorString, nil] forKeys:[NSArray arrayWithObjects: @"DoctorName", nil]];

NSError *error = nil;
NSData *plistData = [NSPropertyListSerialization dataWithPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];

if(plistData){
    [plistDict writeToFile:plistPath atomically:YES];
    NSLog(@"Data saved successfully");
    NSLog(@"Error : %@",error);

}else{
    NSLog(@"Data not happily saved");
}

我的plist结构如下: -

PLIST STRUCTURE

它会返回Data saved successfully消息,但是当我检查plist文件时,它不会保存在那里。

1 个答案:

答案 0 :(得分:0)

根据我对给出的代码的阅读,它只会为DoctorName写下Doctor Bob的值。代码本身没有错误。您可以按照以下方式检查writeToFile方法的返回值。

BOOL ret = [plistDict writeToFile:plistPath atomically:YES];

如果返回值为YES,则将数据写入.plist文件。