读取/写入与应用程序捆绑在一起的 plist 文件

发布于 2024-09-25 03:13:35 字数 2326 浏览 5 评论 0原文

我正在为我的应用程序构建一个附加组件,用户可以在其中搜索预先填充了 .plist 文件中的数据的列表中的项目。它是一个 NSDictionary。如果用户搜索的术语不存在,用户可以点击+按钮并添加它,以便下次出现。

首先,我认为这就像使用 NSUserDefaults 一样简单,但出现了一些问题。

要包含该列表,我必须将其放入捆绑包中,但如果它在那里,我无法向其中添加新的键/值对。我只能对位于“文档”文件夹中的文件执行此操作。

所以我想我必须捆绑 plist,然后在第一次运行时我将它移动到文档文件夹并在那里访问它。

当我需要更新应用程序时,这会出现问题,我想它会覆盖用户输入的值。

是否有一种安全、易于理解、正确的方法来实现我描述的功能?

感谢您提供的任何帮助:)

编辑:**** 实际方法,按照 TheSquad 和 TomH 的建议*****

+ (NSMutableDictionary*) genericProducts {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"GenericProducts.plist"];

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];  
    NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"GenericProducts.plist"];


    if([fileManager fileExistsAtPath:documentPlistPath]){

        NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
        return documentDict;

    } else {

        NSError *error;
        BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];

    if (success) {
        NSMutableDictionary *newlySavedDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
        return newlySavedDict;
    }

    return nil;
}

}

将新产品添加到列表中:

+ (void) addItemToGenericProducts:(NSString*) newProduct {

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"GenericProducts.plist"];
    NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
    [documentDict setObject:newProduct forKey:[MD5Checksum cheksum:newProduct]];

    [documentDict writeToFile:documentPlistPath atomically:YES];

}

I am building an add-on to my app where the user can search for an item in a list that is pre-populated with data from a .plist file. It is an NSDictionary. If the term, the user searched for, does not exist, the user can tap a + button and add it so it is there the next time.

First of I thought it would be as easy as using the NSUserDefaults, but a few problems arises.

To have the list included I must place it in the bundle, but if it is there I can not add new key/value pairs to it. This I can only do with files situated in the Documents folder.

So I guess I have to bundle the plist, then on first run I'll move it to the documents folder and access it there.

This opens up the problem when I need to update the app, I guess it will overwrite the values the user put in.

Is there a secure, easy-understandable, right way to achieve the functionality I describe?

Thanks for any help given:)

Edit: **** the actual approach, as suggested by TheSquad and TomH *****

+ (NSMutableDictionary*) genericProducts {

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"GenericProducts.plist"];

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];  
    NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"GenericProducts.plist"];


    if([fileManager fileExistsAtPath:documentPlistPath]){

        NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
        return documentDict;

    } else {

        NSError *error;
        BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];

    if (success) {
        NSMutableDictionary *newlySavedDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
        return newlySavedDict;
    }

    return nil;
}

}

And for adding a new product to the list:

+ (void) addItemToGenericProducts:(NSString*) newProduct {

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"GenericProducts.plist"];
    NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
    [documentDict setObject:newProduct forKey:[MD5Checksum cheksum:newProduct]];

    [documentDict writeToFile:documentPlistPath atomically:YES];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔姬 2024-10-02 03:13:35

我对我的 sqlite 数据库也有同样的想法...

我最终就是这么做的,将捆绑的文件复制到文档中以便能够修改它。

我所做的是在每次启动时检查该文件是否存在,如果不存在,则复制它。
如果您更新应用程序,文档文件夹将不会被触及,这意味着从以前版本复制的文件仍然存在。

唯一的问题是,如果您希望升级 plist,则必须在应用程序中处理该问题。如果您必须这样做,我建议您使用 NSUserDefault 来检查该应用程序的先前版本是否存在...

I had the same thoughts with my sqlite database...

I end up doing exactly that, copy the bundled file into documents in order to be able to modify it.

What I have done is checking at each startup if the file exist, if it does not, copy it.
If you do an update of your App, the documents folder will not be touch, this means the copied file from the previous version will still be present.

The only issue is that if you want your plist to be upgraded you will have to handle that in your application. If you have to do so I suggest you use the NSUserDefault to check if a previous version of the app existed before...

绝影如岚 2024-10-02 03:13:35

更新应用程序时,文档目录的内容不会更改。

当用户删除应用程序时,文档目录的内容也会被删除。

当应用程序第一次运行时,将一个标志写入 NSUserDefaults。在应用程序的后续运行中,检查该标志是否存在。 (或者,您可以检查文档目录中是否存在 plist)

The contents of the documents directory is not altered when an application is updated.

The contents of the documents directory are deleted when the user deletes the app.

When the app is run the first time write a flag to NSUserDefaults. On subsequent runs of the app, check for existence of the flag. (alternatively, you can just check for existence of the plist in he documents directory)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文