views:

69

answers:

2

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] initWithArray:prefs];

If I try this

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
int tempInt = [def objectForKey:@"addedFavs"];  

will I not need to do type casting?

+2  A: 

You can use -[NSUserDefaults setInteger:forKey:] to store the value and -[NSUserDefaults integerForKey] to retrieve it.

eman
+1  A: 

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"];
Tom Irving