ios - Strange BOOL behavior in tableView (When is 1 not equal to 1?) -


i'm trying convert existing app use core data. i've set entities, , using nsfetchedresultscontroller display data in table view. now, i'm populating database each time app launches.

it appears working fine, aside 1 strange issue. entity has bool value, "showstarcorner" determines whether or not image of star should shown in table cell... so, call similar if statement when configuring each tableview cell:

if (myobject.showstarcorner == [nsnumber numberwithbool:yes]) {     // show star corner } 

but surprisingly results in star being shown on objects created on launch of app. (objects added on previous launches of app should showing star not)

i added test, figure out 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]); } 

... results in else statement getting called added objects:

1 != 1 

as far know, 1 should equal 1... i'm baffled.

i appreciate help. have new objects being created having same attributes on each app launch? (i'm not assigning unique id or anything, sorting results date added)

nsnumber* variables pointers, , == on pointers checks pointer equality. since objects don't share same memory location (as created distinct calls [nsnumber numberwithbool:]), not considered pointer-equal (and therefore comparison returns no).

use isequal: method instead, should check value equality , available on classes, or boolvalue out of nsnumber objects , compare it.

if (myobject.showstarcorner.boolvalue == yes) {     // show star corner }  if ([myobject.showstarcorner isequal:[nsnumber numberwithbool:yes]]) {     // show star corner } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -