我应该使用plist,SQLite还是NsMutableArray?

时间:2009-10-31 04:50:43

标签: iphone sdk

如果我想存储2个字符串,并且ABContact图像(或调用图像的变量)即使在重新启动应用程序后仍然存在,我应该使用哪种方法? NsMutableArray,plist还是SQLite?

4 个答案:

答案 0 :(得分:4)

重新启动应用程序后,NSMutableArray将不会保留。

您可能希望查看存储两个字符串的plist以及指向应用程序的Documents沙箱中缓存的地址簿图像的文件URL。

如果你有很多这些要在应用程序重启之间存储和检索,请查看核心数据。

答案 1 :(得分:4)

对于非常少量的数据,例如几个字符串,但不是图像,您也可以使用更简单的NSUserDefaults。这通常用于保存首选项或某些持久数据。

要保存它:

[[NSUserDefaults standardUserDefaults] aString forKey:@"aStringKey];
if (![[NSUserDefaults standardUserDefaults] synchronize])
    NSLog(@"not successful in writing the default prefs");

阅读:

aString =  [[NSUserDefaults standardUserDefaults] stringForKey:@"aStringKey];

同样,如果您真正需要的是数据库或文件系统,我不会继续使用它。

如果要输入更多数据,而不足以保证SQL数据库或核心数据,那么我会将数据写入plist文件。如果您的所有数据都是对象,那么我会这样做:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:kSavedDocFileName]; 
    NSMutableArray *myArrayToSave = [[NSMutableArray alloc] arrayWithCapacity: 48];
// 48 since you said in the comments that you have 48 of these
    for (int i = 0; i < 48; i++) {
        NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                            sting1[i], @"firstStr", //I'm assuming your strings are in a C array.
                            string2[i], @"secondStr",// up to you to replace these with the right 
                            url[i], @"myURL",       //way to access your 48 pieces of data
                            nil];
        [myArrayToSave addObject:myDict];
        [myDict release];
    }
    if (![myArrayToSave writeToFile:path atomically:YES])
        NSLog(@"not successful in saving the unfinished game");

您可以阅读数据:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:kMySavedDocName];
    NSArray *myLoadedData = [[NSArray alloc] initWithContentsOfFile:path];

    for (int i = 0; i < 48; i++) {
        NSDictionary *myDict = [myLoadedData objectAtIndex:i];
        string1[i] = [myDict objectForKey:@"firstStr"];
        string2[i] = [myDict objectForKey:@"secondStr"];
        url[i] = [myDict objectForKey:@"url"];

    }
    [myLoadedData release];

请确保您不只是复制并粘贴此代码,因为我只是在脑海中输入它。

现在,如果你不想乱用字典,你也可以这样做。这有点不那么优雅了:

保存:

    NSMutableArray *myArrayToSave = [[NSMutableArray alloc] arrayWithCapacity: 48 * 3];
// 48 since you said in the comments that you have 48 of these three objects
    for (int i = 0; i < 48; i++) {
        [myArrayToSave addObject:string1[i]];
        [myArrayToSave addObject:string2[i]];
        [myArrayToSave addObject:url[i]];
    }
    if (![myArrayToSave writeToFile:path atomically:YES])
        NSLog(@"not successful in saving the unfinished game");

负载:

NSArray * myLoadedData = [[NSArray alloc] initWithContentsOfFile:path];

for (int i = 0; i < 48; i++) {
    string1[i] = [myLoadedData objectAtIndex:i * 3];
    string2[i] =  [myLoadedData objectAtIndex:i * 3 + 1];
    url[i] =  [myLoadedData objectAtIndex:i * 3 + 2];

}
[myLoadedData release];

另一个重要的注意事项是,在所有这些例子中,我假设你正在处理对象(48次3)。这就是为什么你可以将它们添加到字典和数组中并保存它们并轻松地重新加载它们。如果你正在处理非对象,如整数,或c字符串或BOOL,那么你将不得不使用[NSNumber ...]将它们转换为对象,然后[对象...]将它们变回非注意开始。

尝试其中一些,然后在〜/ Library / Application Support / iPhone Simulator / Applications文件夹中找到你的app文件夹,查看Documents文件夹,打开你保存的文件,并验证其内容。能够检查你写出的数据并且看看不同方法如何改变内容总是令人放心。

祝你好运!

答案 2 :(得分:1)

不要做数据库。对于那些小数据来说,这样做太过分了。 一个plist就好了。您可能不应该使用NSUserDefaults,我认为这对于太多数据的边界是正确的。只需将plist写入应用程序内的文档目录,并在应用程序重新启动时加载它。您可以将NSMutableArray存储在plist中,但它会以NSArray的形式返回给您。只需使用[NSMutableArray arrayWithArray:]将其重新创建为可变数组并保留它。

答案 3 :(得分:0)

  • (void)doneAceSpadeSetupClick:(id)sender {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *string1;
    NSString *string2;
    NSString *string3;
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"card1.plist"]; 
    NSMutableArray *myArrayToSave = [[NSMutableArray alloc] initWithCapacity: 1];
    //48 since you said in the comments that you have 48 of these
    
          NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:
             string1, characterText.text, //I'm assuming your strings are in a C array.
            string2, actionText.text,// up to you to replace these with the right 
                        string3, objectText.text,
                        nil]; //iPhone simulator crashed at this line
        [myArrayToSave addObject:myDict];
    
    if (![myDict writeToFile:path atomically:YES])
        NSLog(@"not successful in saving the unfinished game");
    
    
    
    [self dismissModalViewControllerAnimated: YES];
    

    }