简体   繁体   English

iOS:为什么没有设置BOOL属性? 财产价值为零

[英]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: 在通知处理程序方法中,我将设置BOOL属性(isNotificationCarryingObject),如下所示:

-(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? 使用1.和2.我无法设置该标志,但是使用3.将其设置为YES,我不知道为什么? According to me all 3 statements should work. 根据我的说法,所有这三个陈述都应该起作用。

isNotificationCarryingObject property is defined as: isNotificationCarryingObject属性定义为:

    @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. 呈现的视图控制器在其-viewWillDisappear方法内发布通知,该通知由呈现的视图控制器接收。

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. po代表print object ,但bool不是对象,而是原始类型。 Those should be printed withe the p -command. 这些应该与p命令一起打印。

The nil object has the address 0x0 , that will evaluate to 0 or NO as-well. nil对象的地址为0x0 ,其结果也将为0NO

How about this 这个怎么样

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

It could also be a concurrency issue from your monatomic attribute. 您的单原子属性也可能是并发问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM