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

dev*_* gr 1 objective-c ios

在通知处理程序方法中,我正在设置一个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 ?????

}
Run Code Online (Sandbox Code Playgroud)

使用1.和2.我无法设置标志但使用3.它设置为YES,我不明白为什么?据我所知,所有3个陈述都应该有效.

isNotificationCarryingObject属性定义为:

    @property (nonatomic, assign) BOOL isNotificationCarryingObject;
Run Code Online (Sandbox Code Playgroud)

通知处理程序在内部呈现视图控制器.在其-viewWillDisappear方法中显示视图控制器发布通知,该方法通过呈现视图控制器来接收.

Vik*_*ica 12

如果你有

BOOL b = NO;
Run Code Online (Sandbox Code Playgroud)

然后你打破程序就行了

po b
Run Code Online (Sandbox Code Playgroud)

你会得到

<nil>
Run Code Online (Sandbox Code Playgroud)

p b 
Run Code Online (Sandbox Code Playgroud)

代替

po代表print object,但bool不是一个对象,而是一个原始类型.那些应该用p-command 打印.

nil对象具有地址0x0,将评估0NO作为.