当BOOL变量实际上是YES iPhone时,它的行为就像NO一样

时间:2011-08-04 15:18:17

标签: iphone objective-c

我是Objective-c的新手,看起来很奇怪。如果名为“isFirstWordBeingAnimated”的BOOL变量等于YES,我不想启用方法。

变量isFirstWordBeingAnimated等于NO,由于某种原因,它被视为YES: enter image description here

请注意,当我将鼠标放在该变量的顶部时,同时调试xcode告诉我isFirstWordBeingAnimated等于NO。为什么返回块被执行了?

此外,我只在一次使用breackpoint的代码上设置了isFirstWordBeingAnimated = YES: enter image description here

我还没有到达那个断点,xCode认为它等于YES,并且在视图加载时我将该变量设置为NO。这是为什么?

可能是因为我还没有定义getter和setter吗?我在.m文件的顶部定义了isFirstWordBeingAnimated而没有创建mutator(setter)和accessor(getter)方法......


修改

我已更改了我的代码:

enter image description here

enter image description here

我遇到了同样的问题:

enter image description here

2 个答案:

答案 0 :(得分:1)

首先,我要在头文件中创建一个属性:

@property (nonatomic, assign, getter= isFirstWordBeingAnimated) BOOL firstWordBeingAnimated;

还要添加到您的主文件中:

@synthesize firstWordBeingAnimated;

接下来我不想使用if(isEnabled) return;

最好将括号放在return;

附近
if (isEnabled) {
     return;
}

尝试if (isEnbabled)代替if (isEnabled==YES),我不会改变任何内容,但我没有其他想法。

答案 1 :(得分:0)

首先,不需要使用isFirstWordBeingAnimated == YES。只需将布尔值粘贴在括号中,它就会被评估为真或假。其次,你创建了getter / setter,但实际上并没有使用它。如果您想引用布尔值或设置它,请使用self.isFirstWordBeingAnimated

关于你的问题,除非你明确地将它设置为true,否则布尔值总是为false。在你的情况下,我认为问题在于你的布尔值永远不会被创建 - 从而使它成为假。如果您不介意更多代码将有助于准确理解您正在做什么。