I’m trying to convert an existing app to use core data. I’ve set up my entities, and am using an NSFetchedResultsController to display data in a table view. For now, I’m just populating the database each time the app launches.
It appears to be working fine, aside from one strange issue. My entity has a BOOL value, “showStarCorner” that determines whether or not an image of a star should be shown in the table cell… so, I call something similar to this if statement when configuring each tableview cell:
if (myObject.showStarCorner == [NSNumber numberWithBool:YES]) {
// show the star corner
}
But surprisingly that results in the star only being shown on the objects I created on that launch of the app. (objects added on previous launches of the app that should be showing the star are not)
I added an extra test, to help figure out what is wrong:
if (myObject.showStarCorner == [NSNumber numberWithBool:YES]) {
NSLog(@"%@ == %@", myObject.showStarCorner, [NSNumber numberWithBool:YES]);
}
else if (myObject.showStarCorner != [NSNumber numberWithBool:YES]) {
NSLog(@"%@ != %@", myObject.showStarCorner, [NSNumber numberWithBool:YES]);
}
… which results in the else statement getting called for the previously added objects:
1 != 1
As far as I know, 1 should always equal 1… so I’m completely baffled.
I would greatly appreciate any help. Could it have something to do with the new objects being created having the same attributes on each app launch? (I’m not assigning any unique ID or anything, just sorting the results by the date they were added)
NSNumber*variables are pointers, and==on pointers checks for pointer equality. Since your objects don’t share the same memory location (as they were created by distinct calls to[NSNumber numberWithBool:]), they are not considered pointer-equal (and therefore the comparison returnsNO).Use the
isEqual:method instead, which should check for value equality and is available on most classes, or get theboolValueout of yourNSNumberobjects and compare it.