2

Should I use NSNumber or string, for saving a simple "playerID number" that now is an integer because I want to save it with Core Data?

In my datamodel I've got a playerID set to Integer 16 but Core Data want's the NSNumber to work. This seems lika a lot of code for "nothing" - example;

NSInteger myValue = 1;
NSNumber *number = [NSNumber numberWithInteger:myValue];
p1.playerID = number;

// Instead of just;
p1.playerID = 1;
// or
p1.playerID.myIntValue;

Just wondering if it would not be easier to set it to string instead and then convert the value (playerID) back and forth as it's needed? Any words of wisdom (experience) on this? Thank's :-)

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Mac
  • 111
  • 1
  • 11

3 Answers3

10

On your entity, define the @property like this:

@property (nonatomic) int32_t playerID;

and then you'll be able to simply set it like

p1.playerID = 1;

This is very elegant and very fast.

In other words, don't define it as being an NSNumber *. But even if you did, performance would still be excellent. It's just easier to read and write code like this. Also note that with the newest Xcode versions, you can use @(myInt) in stead of [NSNumber numberWithInt:myInt].

Make sure to also check out how to use this for enums: Best way to implement Enums with Core Data (look for my answer).

Community
  • 1
  • 1
Daniel Eggert
  • 6,665
  • 2
  • 25
  • 41
  • Is it possible to define this in the datamodel editor? Since I sometimes update the datamodel and then lets the CD editor overwrite the classes... (I mean I might forget to update the class with your solution) – Mac Sep 05 '12 at 15:58
  • I honestly never use the code generator. It's so simply to just type the @property stuff myself. That said, note that this is not part of the model -- the model stays the same. You're just using scalar accessors to the same values. – Daniel Eggert Sep 07 '12 at 06:11
  • Since Core Data uses NSNumber instead of int's simple stuff like adding +1 (myVariable++) becomes a lot more work, even with the new literals (myVariable = @34). But I solved it in the end, thank's for the help! – Mac Sep 07 '12 at 10:10
  • Eric Goldberg provides a bit more context as to what's going on here, and how to get Xcode to do this for you when you generate the NSManagedObject subclass. Visit http://stackoverflow.com/questions/7044816/store-nsinteger-in-core-data – Dalmazio Dec 24 '12 at 00:58
2

NSNumber much more effecient to store value in memory than NSString. Because NSNumber uses a runtime facility called tagged pointers to increase speed and reduce memore usage (See more information)

Also I think that NSNumber takes up less space in CoreData than NSString.

Victor
  • 3,497
  • 3
  • 39
  • 51
0

You can generate your properties with primitive values when creating your NSManagedObject files using Xcode. However, you should know that NSNumber is an abstract class and the runtime class type will depend on what value was used to create the NSNumber.

J2theC
  • 4,412
  • 1
  • 12
  • 14