我使用以下代码来访问数据并将其附加到 plist 中。但每次plist中的数据都会被覆盖。
代码有错误吗?
请帮忙。
NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"stores.plist"]))
{
if ([manager isWritableFileAtPath:plistPath])
{
NSArray * infoArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSMutableArray * newArray = [infoArray mutableCopy];
NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];
[infoDict setObject:@"a object" forKey:@"Lname"];
[infoDict setObject:@"3s3z32 object" forKey:@"Fname"];
[newArray addObject:infoDict];
[newArray writeToFile:plistPath atomically:TRUE];
[manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
}
}
最佳答案
问题是你的plist没有更新任何东西,因为你试图在主包中编写plist,而在主包中你不能写任何东西,所以它可能只采用默认值..
在这里查看我的答案NSDictionary value replacing instead of adding in Plist
关于ios - 每次 Plist 文件都会被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15778895/