views:

53

answers:

1

I have a managedObject with an attribute that is a Boolean. I need to compare the value of this and then hide a button if required.

There's a couple of caveats, firstly the isBookmarkHidden boolean can be set and will override the property of the managedObject so the button is hidden regardless. If this boolean is NO it will then use the ManagedObject.

Here's the code snippet which is just in the viewDidLoad method ...

BOOL shouldHideBookmark = (int)[[managedObject valueForKey:@"isBookmarked"] description];

bookmarkButton.hidden = isBookmarkHidden == YES? YES : shouldHideBookmark == YES? YES : NO;

No matter how I write this code it doesn't seem to work. Any ideas?

+6  A: 

No, that's not correct.

valueForKey: returns an object, and when it's a boolean attribute, it's an NSNumber. So it should be:

BOOL shouldHideBookmark = [[managedObject valueForKey:@"isBookmarked"] boolValue];
bookmarkButton.hidden = (shouldHideBookmark || isBookmarkHidden);
Dave DeLong
See also: http://www.techotopia.com/index.php/Objective-C_Operators_and_Expressions#Boolean_Logical_Operators
Douglas
thanks very much for your response. Works a treat.
Lee Probert