谈到BOOL值在Objective-C的方法 [英] Referring to BOOL values in Objective-C methods

查看:116
本文介绍了谈到BOOL值在Objective-C的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code的一小部分:

this is a small portion of the code:

@interface
BOOL isCarryingmallet;

@implementation

-(BOOL)isCarryingWeapon {
   return isCarryingMallet;
}

-(int)getWeaponDamage {
    if (isCarryingMallet) 
        return kVikingMalletDamage;
    else
        return kVikingFistDamage;
}

我不明白这是如何工作。是否返回isCarryingmMallet; 返回YES或NO?为什么没有一个 = = YES 如果在(isCarryingMallet)?为什么如果(isCarryingMallet)不是如果(isCarryingWeapon)
谢谢回答我的问题福利局!

I don't understand how this works. Does return isCarryingmMallet; return YES or NO? Why isn't there an == YES in if (isCarryingMallet)? Why is it if (isCarryingMallet) not if (isCarryingWeapon). Thanks for answering my newb questions!

推荐答案

isCarryingWeapon 方法返回的值 isCarryingMallet 变量。如果有其他可能的武器,这个方法可以返回更复杂的值,例如(isCarryingMallet || isCarryingPlasmaCannon)。基本上现在在意的这个类的唯一武器是槌。当 isCarryingWeapon 方法被调用,这个类对自己说,我是背着槌?如果是这样,它知道它的携带武器,所以它返回true - 是的,我有一个武器否则返回false。

The isCarryingWeapon method returns the value of the isCarryingMallet variable. If there were other possible weapons, that method could return a more complicated value, like (isCarryingMallet || isCarryingPlasmaCannon). Basically right now the only weapon that matters to this class is the mallet. When the isCarryingWeapon method is called, this class says to itself, "Am I carrying a mallet?" If so, it knows it's carrying a weapon, so it returns true - "Yes, I've got a weapon." Otherwise it returns false.

对于如果的问题 - 所有的如果语句做的是确定在括号中的值是否计算为真还是假的。平等语句在if语句共同pretty但他们根本不需要。更具体地,任何不是0,null或零是真实的。所以下面都将在大括号执行code:

As for the if question - all if statements do is determine whether the value in the parentheses evaluates to true or false. Equality statements are pretty common in if statements but they're not at all required. More specifically, anything that is not 0, null, or nil is true. So the following will all execute the code in the curly braces:

if (1) {...}
if (37) {...}
if (YES) {...}
if (true) {...}

布尔变量往往与preFIX名为使这使得自然的语法意义。你的情况,你可以解析

Boolean variables are often named with the prefix is so that this makes natural grammatical sense. In your case, you can parse

if (isCarryingMallet) {...}

如果这家伙拎着木槌,然后...

If this guy is carrying a mallet, then...

这篇关于谈到BOOL值在Objective-C的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆