int age == 3;
if (age == 2)
NSLog(@"2");
if (age == 4)
NSLog(@"4");
if (age == 3)
NSLog(@"3");
I was wondering what’s the point of putting else ifinstead of having three if statements.
for (GameObject *character in listOfGameObjects) {
if (character.myGameObjectType == kPowerUpTypeHealth) {
characterHealth = 100.0f;
}
}
This is in the Viking.m file. character and self(viking) both have the instance variable, setter and getter method characterHealth. If I want to make Viking’s health equal to a hundred do I have to write self.characterHealth = 100.0f;to make sure the health isn’t added to character? Or is what I already have fine?
Thank you!
The thing is that if you have 3 if statements, all of those conditions will be checked, since age can only have 1 value, you should use
so after one of those conditions is met, the other conditions will NOT be checked, saving processing time.
Regarding the self, it will work both ways, however if characterHealth is a
@property, using self will assing it using the@synthetize setter, so if you have it declared asretainit will increase theretaincount by 1, if you assign it without self it wont increase theretaincount. Although since you are assigning it aprimitiveit doesn’t matter in this case, onlyobjectshave a retain count.