Bhavesh
Bhavesh

Reputation: 1422

BOOL is returning YES for value 0 in objective-c?

I have Invisible = 0 in my model and I am trying get this in BOOL using this line.

BOOL invisible = [self.model objectForKey:@"Invisible"];

but invisible is YES instead of NO. I've also tried with bool but it is also true. I checked class of the object using [[self.model objectForKey:@"Invisible"] class] and it is showing _NSCFBoolean. What's wrong here? model is NSDictionary.

enter image description here

UPDATE

Okay so I found the real issue. I have Filter class where I have getter isInvisible and code is

- (BOOL)isInvisible {
     return [[self.model objectForKey:@"Invisible"] boolValue]; 
   }

When I call this first time it returns me NO. Which is good. Now immediately after that I call it again and it returns <nil>. Strange.

(lldb) po filter.isInvisible 
NO 
(lldb) po filter.isInvisible 
<nil>

There is only 1 second difference between two po commands.

Upvotes: 1

Views: 993

Answers (2)

Natarajan
Natarajan

Reputation: 3271

Since you return BOOL value from isInvisible method, I believe nil is equivalent to 0. So 0 is equivalent to NO or false. hence filter.isInvisible should be NO or false.

Please just verify that in if statement in your code.

Example:

if (filter.isInvisible == NO){//Should be reached here}

Upvotes: 0

trapper
trapper

Reputation: 12003

= [self.model objectForKey:@"Invisible"];

This will return an NSNumber object. If you want a BOOL primitive you can get it via the boolValue method so

= [[self.model objectForKey:@"Invisible"] boolValue];

Upvotes: 4

Related Questions