dev gr
dev gr

Reputation: 2431

iOS: Why BOOL property is not getting set? Value of property is nil

In a notification handler method I am setting a BOOL property (isNotificationCarryingObject) like below:

-(void)notificationReceived:(NSNotification *)notification
{
    //set flag depending upon if notification carries an object
    //I tried following:

    //1.
   self.isNotificationCarryingObject = notification.object != nil;
   //result: self.isNotificationCarryingObject = nil

   //2.
   self.isNotificationCarryingObject = notification.object != nil ? YES : NO;
   //result: self.isNotificationCarryingObject = nil

  //3.
  self.isNotificationCarryingObject = YES;
  //result: self.isNotificationCarryingObject = YES ?????

}

Using 1. and 2. I am not able to set the flag but using 3. it gets set to YES, I don't understand why? According to me all 3 statements should work.

isNotificationCarryingObject property is defined as:

    @property (nonatomic, assign) BOOL isNotificationCarryingObject;

Notification handler is inside presenting view controller. Presented view controller posts notification inside its -viewWillDisappear method which gets received by presenting view controller.

Upvotes: 1

Views: 2421

Answers (2)

vikingosegundo
vikingosegundo

Reputation: 52227

If you have

BOOL b = NO;

and you break the program after it and do

po b

you'll get

<nil>

do

p b 

instead

po stands for print object, but a bool is not an object, but a primitive type. Those should be printed withe the p-command.

The nil object has the address 0x0, that will evaluate to 0 or NO as-well.

Upvotes: 13

bhuvy
bhuvy

Reputation: 304

How about this

if(notification.object)
notificationFlag=YES;
else
notificationFlag=NO;

It could also be a concurrency issue from your monatomic attribute.

Upvotes: -1

Related Questions