无法保存数据到PList
下面是我的方法来保存数据:无法保存数据到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结构如下: -
它返回Data saved successfully
消息,但是当我在检查plist文件,它不会保存在那里。
根据我给出的代码的读数,它只会将DoctorName
的密钥写为值Doctor Bob
。代码本身没有错误。您可以按照以下方法检查writeToFile
方法的返回值。
BOOL ret = [plistDict writeToFile:plistPath atomically:YES];
如果返回值是将数据写入到文件的.plist。
返回值是1,但是当我检查plist文件时,没有任何变化。 – user1498840
我刚刚运行代码,它工作正常,并正确地保存在plist文件中的值。您的代码中必须存在其他问题,请尝试仅运行此代码以检查是否保存该代码。 –
它会覆盖我的plist数据还是会追加? – user1498840
对'writeToFile:'调用的返回值是什么? – rmaddy
@rmaddy通向doctorprofile.plist – user1498840
的路径。不是'YES'或'NO'。检查'writeToFile:'的返回值。 – rmaddy