ios - 每次 Plist 文件都会被覆盖

标签 ios objective-c xcode plist

我使用以下代码来访问数据并将其附加到 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/

相关文章:

ios - Storyboard - 打开 UIViewController 作为 UIPopoverController

ios - iOS 中 UIImageView 的动画圆形 mask

objective-c 除法总是返回 0

objective-c - 如何在带有 UITextField 和 UITextView 的 Objective-C 中使用外部 C 函数 ;-)

xcode - 如何缩短我在 Xcode 中的构建/归档时间?

ios - 双击手势功能仅在第一次点击后触发,每隔一次它需要 2 次点击

ios - Collection View定位像instagram app swift

ios - 使用 Storyboard在同一容器 View 中的多个 View

ios - 尝试实现 TableView 并获取线程 1 : signal SIGABRT error?

ios - 迭代传递给 View 的核心数据