views:

254

answers:

1

I've got the following line of code in one of my Objective-C methods:

if (self.rule.sunday == YES) { do some stuff... }

The line produces the following compiler warning:

Comparison between pointer and integer

It's just a warning, so it's not life-threatening, but it's driving me nuts. Clearly there is some basic concept about integers, pointers, and/or booleans that I am missing. Can somebody please enlighten me so I can fix my code?

As usual, thanks in advance for your help.

UPDATE: For reference, the corrected code looks like this:

if ([self.rule.sunday boolValue] == YES) { do some stuf... }
+2  A: 

self.rule.sunday is returning an object reference. YES is not an object, it's an unsigned char. So you're comparing an object reference (ie, a memory address) to 1. Of course it's going to complain at you.

Dave DeLong
Ok...but if the object being referred to is a boolean, how are you supposed to compare against it for decision-making purposes? The construct I'm shooting for is, "If the boolean 'sunday' attribute of the Rule entity called 'rule' is 'yes' then do some stuff."
Andy
@Andy there is no such thing as a boolean object (unless you've created one). BOOLs are commonly wrapped in `NSNumber` objects, so in that case you'd use `-boolValue` to extract the `BOOL`. But a better question is: how are you declaring the `sunday` property on the `rule` type?
Dave DeLong
I'm using Core Data to manage the objects; the "sunday" attribute is marked as a "Boolean."While I was waiting I came across the NSNumber Class reference and the -boolValue and +numberWithBool methods. Substituting those on the lines in question took care of all of the compiler warnings, and eliminated another bug that I hadn't even started working on yet.Thanks for the shove in the right direction, Dave.
Andy
@Andy yep, that makes sense. I think the easiest way to fix this would be `if ([self.rule.sunday boolValue] == YES) { ... }`, since that means you won't have to be creating a ton of `[NSNumber numberWithBool:YES]` objects. Glad I could help! :)
Dave DeLong
That's exactly how I did it. The only places I used the numberWithBool method was where a slice of code needed to update the 'sunday' attribute. Thanks again.
Andy