Reputation: 179
this is the code:
NSNumber *taskId = [[self.taskList objectAtIndex:indexPath.row] valueForKey:@"identity"];
NSInteger *intTaskId = [[self.taskList objectAtIndex:indexPath.row] valueForKey:@"identity"];
self.taskList
is an NSArray
which filled with core data fetch request in ViewController's viewDidLoad
method.
the taskId
is: 1
the intTaskId
is: 269303816
In actually, the value stored in core data is: 1
below is my questions:
1, I am confused why the NSInteger
incorrect?
2, Should I have to replace NSInteger
with NSNumber
to avoid any other problems?
Upvotes: 0
Views: 261
Reputation: 10782
NSNumber
is an object, whereas NSInteger
is simply a typedef
for a primitive (non-object) type (like int
). NSInteger
is not a subclass of NSNumber
. Core Data returns numbers as instances of NSNumber
. You're getting the weird NSInteger
value because it's pointing to an object of type NSNumber
but attempting to print it as if it were just an integer.
You'll need to replace NSInteger
with NSNumber
to avoid any problems. You could also use the intValue
method on NSNumber
to get back an NSInteger
:
NSNumber *objTaskId = [[self.taskList objectAtIndex:indexPath.row] valueForKey:@"identity"];
NSInteger *intTaskId = [objTaskId intValue];
You'll need to do this if you want to do comparisons (greater than, equal too, smaller than) or arithmetic (you can't add an NSNumber
to another NSNumber
or an NSNumber
to a primitive type like an int
or float
).
Upvotes: 3