Is there any way I can skip dealing with NSNumber and work directly with NSInteger?
Asked
Active
Viewed 5,386 times
2 Answers
3
Core Data will only allow NSNumbers. However, you can write custom getters and setters to use NSInteger properties. mogenerator is a wonderful tool that does that automatically for you: it generates classes with native properties for all your entities.

ndfred
- 3,842
- 2
- 23
- 14
-
5Also in the current version (4.3.2 as of this comment) of Xcode, you can check the box when generating NSManagedObject subclass to "use scalar properties for primitive data types" -- which will cause the compiler to "just know" to make the synthesized accessors promote/demote to/from NSNumber and the proper scalar. For instance, "Integer 32" will be declared as int32_t in your generated .h file, and you can simply assign integers to the property in your code. – Eric G Apr 06 '12 at 21:47
2
No. NSInteger
is just a typedef for a long integer, not an object.
Actual implementation:
#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
The NSNumber
class allows the encapsulation of primitive types (int
, float
, etc.) into an object, which can then be stored into Property Lists and Core Data.
Example:
float pi = 3.1415;
NSNumber *piNumber = [NSNumber numberWithFloat:pi];
You can then easily access and/or transform the value stored into the NSNumber
object:
int piAsInteger = [piNumber intValue];

Evan Mulawski
- 54,662
- 15
- 117
- 144
-
-
Yes, it is. Core Data only holds *objects*, which is why the `NSNumber` class is required. There is no other alternative without unreliable third-party utilities, which can break with any update. – Evan Mulawski Aug 12 '11 at 19:04
-
1He asked if he could skip NSNumber and use NSInteger. The answer is no, because you can only store objects in core data. A description of how NSNumber encapsulates values is superfluous. And if someone else disagrees with me they are free to upvote your answer. – Abizern Aug 12 '11 at 19:07
-
I think a) you need to reread the question - he's already using NSNumber, he wonders if he can skip it. An explanation is off-topic (IMHO and i've voted accordingly) b) you shouldn't be so sensitive to downvotes. I didn't find your answer useful so I downvoted it. Anyone else is welcome to make their own voting decisions. – Abizern Aug 12 '11 at 19:11
-
a) I answered that question in the first two sentences. Showing the implementation of `NSInteger` was proof to back up that answer. b) I am not sensitive to downvotes, only downvotes without proper reasoning. – Evan Mulawski Aug 12 '11 at 19:18
-
So, you answer is explaining difference between NSInteger and NSNumber, but I've already knew that. And I asked how can i skip NSNumber, not what the difference between them. – Andoriyu Aug 12 '11 at 19:34
-
1@Andoriyu: And my first word is No and I explained why. What is so difficult to understand? – Evan Mulawski Aug 12 '11 at 19:35