1

我的应用程序是用户可以评分和记笔记等的项目列表。所有这些项目(存储用户评分和笔记的字典)都存储在一个 plist 中,当应用程序首次加载时,该 plist 将移动到文档目录(如果文件目录中已经存在 plist 文件,则跳过此步骤)。现在我有一些评论,人们一直在说列表中应该有更多的项目。现在我试图弄清楚如何将新项目添加到 plist。如果我将它们添加到现有 plist 中,用户将永远不会看到新项目,因为应用程序会检查数据库是否已存在于文档目录中。所以他们永远不会收到新的名单。所以我计划创建一个包含项目的新 plist,如果项目没有,则将每个新项目附加到旧 plist t 已经存在(因为用户可以自己添加项目,所以我不希望有重复的条目,以防用户添加了同名的项目)。我的困境是我不确定在哪里/如何进行 plist 合并。我是否在 App Delegate 中执行此操作?我是否在viewDidLoad的主要观点?如果应用程序已将新项目添加到旧 plist 中,如何阻止它在每次加载应用程序时进行检查?我知道有不同的方法可以做到这一点。我只是在寻找最简单且“应用程序负载更少”的方法。

4

1 回答 1

0

行。感谢其中一个 iOS 聊天中的几个人,我解决了这个问题。所以基本上在我的application didFinishLaunchingWithOptions方法中,我AppDelegate.m将两个 plist 加载到 2 个不同的NSArraysarray1 和 array2 中,我还创建了一个 NSMutableArray array3,我用用户文档目录中的原始 plist 加载它。然后我循环检查它们以确保我没有根据它们添加任何重复的项目Name_Key(因为数组中的项目是字典)。如果项目不匹配,那么我会将它们添加到第三个数组(array3)。然后我将array3保存到原始plist文件路径中。然后我在用户默认值中设置了一个整数,所以它只会执行一次这个任务。我不希望应用程序在用户每次打开应用程序时都经历这个。下面是我使用的代码。

int haveMergedPlist = [[NSUserDefaults standardUserDefaults] integerForKey:@"merged plist"];

if (haveMergedPlist < 1){
    // I used a integer rather than a BOOL because if I ever want to perform this operation again with another app update all I have to do is change the number.

    // merge code here

    //new plist
      NSString *newPlistPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"NewPlist.plist"];
      NSArray *newPlistArray = [[NSArray alloc]initWithContentsOfFile:newPlistPath];

    //original plist in user documents directory
      NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [searchPaths lastObject];
      NSString *originalPlistPath = [documentsDirectory stringByAppendingPathComponent:@"originalPlist.plist"];
      NSArray *originalPlistArray = [[NSMutableArray alloc]initWithContentsOfFile:originalPlistPath];
      NSMutableArray *combinedPlistArray = [[NSMutableArray alloc]initWithContentsOfFile:originalPlistPath];

    // Loop through both plists arrays and add if it does not match/exist.
      BOOL itemExists = YES;
    for (NSDictionary *newItem in newPlistArray) {

        for (NSDictionary *originalItem in originalPlistArray) {
            NSString * newItemNameString = [[newItem objectForKey:NAME_KEY] lowercaseString];
            NSString * origItemNameString = [[originalItem objectForKey:NAME_KEY] lowercaseString];

           //Compare lowercase strings and if they don't match then add them to the original plist array.
            if ([newItemNameString isEqualToString:origItemNameString]) {
                itemExists = YES;
                break;
            } else {
                itemExists = NO;
                //doesn't match so add it
            }
        }
        if (itemExists == NO) {
            [combinedPlistArray addObject:newItem];
            NSLog(@"newItem added");
            itemExists = YES;
        }

    }
    //Write Array With New Items Added to documents directory
    NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"originalPlist.plist"];
    [combinedPlistArray writeToFile:writeableDBPath atomically:YES];

    //set integer to 1 in user defaults so it only merge's once
    //I comment this out while getting the loops to work otherwise you will have to delete the app out of the simulator or the phone to make sure its working every time.
    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"merged plist"];
}
于 2013-01-14T21:18:40.430 回答