开发者

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"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜