[英]Should I use plist, SQLite or NsMutableArray?
重新启动应用程序后, NSMutableArray
将不会保留。
您可能需要查看一个plist,其中存储了两个字符串和一个指向URL的文件URL,该URL缓存在应用程序的Documents沙箱中。
如果在应用程序重新启动之间有很多要存储和检索的内容,请查看Core Data。
对于非常少量的数据(例如一些字符串,但不是图像),也可以使用简单得多的NSUserDefaults。 这通常用于保存首选项或某些持久性数据。
要保存它:
[[NSUserDefaults standardUserDefaults] aString forKey:@"aStringKey];
if (![[NSUserDefaults standardUserDefaults] synchronize])
NSLog(@"not successful in writing the default prefs");
阅读:
aString = [[NSUserDefaults standardUserDefaults] stringForKey:@"aStringKey];
同样,如果您真正需要的是数据库或文件系统,我不会继续使用它。
如果要放入更多数据,但不足以保证SQL数据库或Core Data,那么我会将数据写入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分配] 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];
另一个重要的注意事项是,在所有这些示例中,我假设您正在处理对象 (其中的3个对象乘以48)。 这就是为什么您可以将它们添加到字典和数组中,然后保存并重新加载它们的原因。 如果您要处理非对象,例如int或c字符串或BOOL,则必须使用[NSNumber ...]将其转换为对象,然后使用[object ...]将其转换为非对象。首先开始。
尝试其中的一些,然后在〜/ Library / Application Support / iPhone Simulator / Applications文件夹中找到您的应用程序文件夹,然后在Documents文件夹中查找,打开已保存的文件,并验证其内容。 总是可以放心的是,能够检查您写出的数据,并查看不同方法如何更改内容。
祝好运!
不要做数据库。 对于这么少的数据,这太过分了。 一个plist就好了。 您可能不应该使用NSUserDefaults,我认为这正好在太多数据的边界上。 只需将plist写入应用程序内的documents目录,然后在应用程序重新启动时加载它。 您可以将NSMutableArray存储在plist中,但它会作为NSArray返回。 只需使用[NSMutableArray arrayWithArray:]将其重新创建为可变数组,然后将其保留即可。
(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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.