开发者

iOS: NSUserDefaults Warning When I Try to Insert an "int" Value

In my app I wish to save at some point an "int" value in NSUserDefaults, and it gives me warning about the value not being of an "id" type and being an "int". i don't get this warning if I try to insert an NSString.

Here are the details.

The code I have written:

int currentCategoryForUserDefaults = currentCategory; //"cuurentCategory" is an enu开发者_StackOverflowm value.

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:currentCategoryForUserDefaults forKey:@"currentCategoryForUserDefaults"];

The warning I get (for the 3rd line):

Semantic Issue : Incompatible integer to pointer conversion sending 'int' to parameter of type 'id'

Anyone?


Use setInteger:forKey: instead of setObject:forKey:. An integer is not an object, it's a primitive type.

Then, you can retrieve it with integerForKey:.

Another option is to wrap your integer into a NSNumber object.


Try use - (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName instead or wrap your int value into NSNumber and this will be exactly what debugger wants from you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜