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?
NSNumberis an object, whereasNSIntegeris simply atypedeffor a primitive (non-object) type (likeint).NSIntegeris not a subclass ofNSNumber. Core Data returns numbers as instances ofNSNumber. You’re getting the weirdNSIntegervalue because it’s pointing to an object of typeNSNumberbut attempting to print it as if it were just an integer.You’ll need to replace
NSIntegerwithNSNumberto avoid any problems. You could also use theintValuemethod onNSNumberto get back anNSInteger: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
NSNumberto anotherNSNumberor anNSNumberto a primitive type like anintorfloat).