在我的应用中,我需要保存一个双倍值(高分)和字符串(播放器名称)我应该用什么来获得它。
任何想法都会很棒。
谢谢
答案 0 :(得分:3)
如果这就是你要保存的全部,那么NSUserDefaults应该没问题
// To store
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@"name"];
[[NSUserDefaults standardUserDefaults] setDouble:score forKey:@"score"];
// To read back in
NSString *name = [[NSUserDefaults standardUserDefualts] objectForKey:@"name"];
double score = [[NSUserDefaults standardUserDefaults] doubleForKey:@"score"];
// Don't forget that your name is autoreleased - if you want to keep it, set it to a retained
// property or retain it yourself :)
答案 1 :(得分:1)
正如deanWombourne所说,你可以使用NSUserDefaults来存储这些数据,但它不是很安全。如果您不想“在空中”存储这些数据,可以查看Buzz Andersen的SFHFKeychainUtils将它们存储在iPhone Keychain中。
首先,将SFHFKeychainUtils文件复制到您的项目中。单击SFHFKeychainUtils.m并单击Get Info。转到“目标”选项卡,检查目标附近的框是否已选中。如果没有,请检查它。按住Control键并单击Framework文件夹,然后选择Add Existing Framework。找到Security.framework并将其添加到您的项目中。还要检查是否通过对SFHFKeychainUtils.m执行相同的过程将此框架添加到目标中。现在打开您要使用此代码的实现文件,并添加到顶部#import "SFHFKeychainUtils.h"
。
这是关于如何使用此代码的一个小例子:
// to store your data
NSError *error = nil;
[SFHFKeychainUtils storeUsername:kName andPassword:name forServiceName:kStoredName updateExisting:YES error:&error];
[SFHFKeychainUtils storeUsername:kScore andPassword:score forServiceName:kStoredScore updateExisting:YES error:&error];
// to get them back
NSString *name = [SFHFKeychainUtils getPasswordForUsername:kName andServiceName:kScoredName error:&error];
double score = [SFHFKeychainUtils getPasswordForUsername:kScore andServiceName:kScoredScore error:&error];
// kName, kScore, kStoredName, kStoredScore are defined key but you can use also strings with @"your string here".
// It is important that when you store and get back a value, username and serviceName must be the same.