Retrieving an int from NSUserDefaults
How do I retrieve an int from NSUserDefaults?
I have the following code
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSArray *prefs = [def arrayForKey:@"addedFavs"];
favList = [[NSMutableArray alloc] initWithA开发者_运维知识库rray:prefs];
If I try this
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
int tempInt = [def objectForKey:@"addedFavs"];
will I not need to do type casting?
You can use -[NSUserDefaults setInteger:forKey:]
to store the value and -[NSUserDefaults integerForKey]
to retrieve it.
Have you looked in at NSUserDefaults in the documentation? There is a integerForKey:
method that will retrieve an integer for a given key.
NSInteger tempInt = [[NSUserDefaults standardUserDefaults] integerForKey:@"aKey"];
精彩评论