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;
}
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!
The
isCarryingWeaponmethod returns the value of theisCarryingMalletvariable. 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 theisCarryingWeaponmethod 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.As for the
ifquestion – allifstatements 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:Boolean variables are often named with the prefix
isso that this makes natural grammatical sense. In your case, you can parseas