According to Stanford University Objective-C course Fall 2010/2011, lecture 3:
typedef struct {
float x;
float y;
} Point;
@interface Bomb
@property Point position;
@end
@interface Ship : Vehicle {
float width, height;
Point center;
}
@property float width;
@property float height;
@property Point center;
- (BOOL)getsHitByBomb:(Bomb *)bomb;
@end
@implementation Ship
@synthesize width, height, center;
- (BOOL)getsHitByBomb:(Bomb *)bomb
{
float leftEdge = self.center.x - self.width/2;
float rightEdge = ...;
return ((bomb.position.x >= leftEdge) &&
(bomb.position.x <= rightEdge) &&
(bomb.position.y >= topEdge) &&
(bomb.position.y <= bottomEdge));
}
@end
Why aren’t float leftEdge and float rightEdge there at the interface?
Also, the return case, it means that if all those cases are right then it returns YES, if not then NO (or 1 if cases are true, 0 if false). Right?
leftEdgeandrightEdgeare not in the interface because they’re local variables. They are only needed in that function.On your second question, you are right. That’s exactly the case.
You only put variables on the interface (class) when you need to make them part of the object being represented by it. Example: if your interface represents a vehicle, then probably
numberOfWheelswill be a interface (class) variable. When you only need a variable in a certain scope (a function) to do a temporary calculation (likeleftEdgeandrightEdgein your example) then all you need is a local variable.