Reputation: 865
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?
Upvotes: 2
Views: 96
Reputation: 3228
There's a difference between local variables and member variables. The member variables are "global" to the class and can be referenced from any instance method since they are part of the interface. leftEdge
and rightEdge
are both local variables and are declared in the getsHitByBomb:
method. They go out of scope as soon as the method returns/exits.
Upvotes: 4
Reputation: 181280
leftEdge
and rightEdge
are 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 numberOfWheels
will be a interface (class) variable. When you only need a variable in a certain scope (a function) to do a temporary calculation (like leftEdge
and rightEdge
in your example) then all you need is a local variable.
Upvotes: 4