以下是我保存数据的方法:
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结构如下: -
它会返回Data saved successfully
消息,但是当我检查plist文件时,它不会保存在那里。
答案 0 :(得分:0)
根据我对给出的代码的阅读,它只会为DoctorName
写下Doctor Bob
的值。代码本身没有错误。您可以按照以下方式检查writeToFile
方法的返回值。
BOOL ret = [plistDict writeToFile:plistPath atomically:YES];
如果返回值为YES,则将数据写入.plist文件。