在我的董事会结束时,我想将级别,董事会积分和总积分保存到一个NSUserDefaults中,然后召回并打破这些数字。这可能吗?只是尝试不每次都有一个NSUserDefaults列表。
示例:
int level = 35
int boardPoints = 500
int totalPoints = 18000
我想保存一个" levelValue"所有三个,也许有一个句点分隔符? levelValue = 35.500.18000;
// Save value
[[NSUserDefaults standardUserDefaults] setObject:levelValue forKey:@"levelValueKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Recall
// Somehow recall Left(levelValueKey,2), Mid(starting from period,2), and right(up to period)
level = [[NSUserDefaults standardUserDefaults] objectForKey:@"levelValueKey"];
boardPoints = [[NSUserDefaults standardUserDefaults] objectForKey:@"levelValueKey"];
totalPoints = [[NSUserDefaults standardUserDefaults] objectForKey:@"levelValueKey"];
有什么想法吗?
答案 0 :(得分:2)
将所有内容保存为NSUserDefaults中的NSArray,如下所示:
//your ints
int level = 35;
int boardPoints = 500;
int totalPoints = 18000;
//put ints in array
NSArray *levelValueArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:level], [NSNumber numberWithInt:boardPoints], [NSNumber numberWithInt:totalPoints], nil]
//save in NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:levelValueArray forKey:@"levelValueKey"];
//retreive
NSArray *arrayLevelValues = [[NSUserDefaults standardUserDefaults] objectForKey:@"levelValueKey"];
使用它们:
NSLog(@"arrayLevelValues: %@", arrayLevelValues[1]);
//the object 1 is a string so you'll need to convert it to int using intValue
int tempPoints = [arrayLevelValues[1] intValue];
NSLog(@"tempPoints: %d",tempPoints);