-5

Here is my code:

CategoryLoaded =  [[NSUserDefaults standardUserDefaults] integerForKey:@"CategorySaved"];

Here is the error:

"Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int'

How do I resolve this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

1

From the SDK header:

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

So you see, NSInteger, which is what you get as the return type from integerForKey:, can be either an int or a long, which is different sizes. You're building 64 bit, most likely, which means you're assigning a long to what is, apparently, an int.

So either change the type of CategoryLoaded to be big enough:

NSInteger CategoryLoaded;

...or use a c-style cast that says "shut up I know what I'm doing":

CategoryLoaded = (int)[[NSUserDefaults standardUserDefaults] integerForKey:@"CategorySaved"];
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
0

Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int'

One way to deal with this is to declare CategoryLoaded as NSInteger rather than int.

Another is to store the number as a NSNumber using +numberWithInt: and use -objectForKey: instead of -integerForKey:.

Caleb
  • 124,013
  • 19
  • 183
  • 272