iOS:为什么没有设置 BOOL 属性?属性(property)值(value)为零

标签 ios objective-c

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

}

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

isNotificationCarryingObject 属性定义为:
    @property (nonatomic, assign) BOOL isNotificationCarryingObject;

通知处理程序位于呈现 View Controller 内。呈现的 View Controller 在其 -viewWillDisappear 方法中发布通知,该方法通过呈现 View Controller 接收。

最佳答案

如果你有

BOOL b = NO;

然后你在它之后打破程序并做
po b

你会得到
<nil>


p b 

反而
po代表 print object ,但 bool 不是对象,而是原始类型。这些应该用 p 打印。 -命令。
nil对象的地址为 0x0 , 这将评估为 0NO以及。

关于iOS:为什么没有设置 BOOL 属性?属性(property)值(value)为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25037547/

相关文章:

objective-c - 从 sleep 状态唤醒时触发登录窗口重新加载 AskForPassword

iphone - 有选择地将 UITextView 添加到 UITableView

ios - 是否可以为可达性功能设置背景?

iphone - objective-c : a C int array in a class interface?

ios - 应用程序构建在物理设备上,但不是模拟器(没有这样的模块)

ios - 2个相同的 bundle ID可以引起任何冲突吗?

ios - 为什么我的应用程序因内容评级与其他类似应用程序相同而被 Apple 拒绝?

ios - 如何使我的单例类可扩展?

php - 如何运行 php 脚本来发送推送通知

ios - 如何调整 UITextView 的大小,使其根据内容自动调整其宽度和高度,并在达到特定值时固定宽度?