我制作了像这样的UMsgs.plist文件
并调用以下方法
-(void) checkIsChangedUMsg
{
BOOL isNewMsg = NO;
NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
destPath = [destPath stringByAppendingPathComponent:@"UMsgs.plist"];
// If the file doesn't exist in the Documents Folder, copy it.
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:destPath]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"UMsgs" ofType:@"plist"];
[fileManager copyItemAtPath:sourcePath toPath:destPath error:nil];
}
// Load the Property List.
NSMutableArray *dataArray = [[NSMutableArray alloc] initWithContentsOfFile:destPath];
for( int i = 0; i < [dataArray count] ;i++)
{
NSString * oldMessengerID = [dataArray[i] objectForKey:@"messengerid"];
NSString * oldMessageCount = [dataArray[i] objectForKey:@"messageCount"];
NSString * newMessengerID = [UMsgsInfoArray[i] objectForKey:@"messengerid"];
NSString * newMessageCount = [UMsgsInfoArray[i] objectForKey:@"messageCount"];
if( !([oldMessengerID isEqualToString:newMessengerID] &&
[oldMessageCount isEqualToString:newMessageCount]) )
{
NSDictionary * newDict = [[NSDictionary alloc] initWithObjectsAndKeys:newMessageCount,@"messageCount",newMessengerID,@"messengerid",nil];
dataArray[i] = newDict;
isNewMsg = YES;
}
}
if(isNewMsg)
{
BOOL success = [dataArray writeToFile:destPath atomically:YES];
}
}
dataArray是
但是[dataArray writeToFile:destPath atomically:YES];返回NO
为什么它不起作用?我该怎么解决?
答案 0 :(得分:2)
看起来 dataArray 包含一个或多个非属性列表对象的对象,(更具体地说, NSNull 对象) 。如果是这种情况, writeToFile:atomically:将返回 NO 。
文档提到了以下关于 writeToFile:atomically:
的内容此方法在写出文件之前以递归方式验证所有包含的对象是属性列表对象,如果所有对象都不是属性列表对象,则返回NO,因为生成的文件不是有效的属性列表。
此外,您可能想尝试制作NSArray的可变副本,然后在尝试写入文件之前制作不可变副本。
例如
// Load the Property List.
NSMutableArray *dataArray = [[[NSArray alloc] initWithContentsOfFile:destPath] mutableCopy];
并在写作之前使其变为不可变 -
BOOL success = [[dataArray copy] writeToFile:destPath atomically:YES];