Learn efficiently and remember over time.
Start Long-Term Learning
Add this set to a folder
The interface file (.h) tells the computer what a Fraction class looks like: it has six instance methods. The first three methods don't return a value. The next two return an int, and the last one returns a double. The setNumerator: and setDenominator: methods each take an integer argument.
Synthesized Accessor Methods
As of Objective-C 2.0, you can
have your setter and getter methods (collectively known as accessor
methods) automatically generated for you
1. create the @property in the interface file
2. create the @synthesize in the implementation file
@synthesize numerator, denominator
Creates four methods:
You don't need to use the @synthesize directive. If you don't, the instance variables will have an _ as the first character of its name.
The Dot Operator
Used to access properties:
is the same as
instance.property = value
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
myFraction.numerator = 1;
myFraction.denominator = 3;
Method with multiple arguments
Simply set each extra argument with a colon following it
[myFraction setNumerator: 1 andDenominator: 3];
[myFraction set: 1 over: 3]
[aFraction set:1 :3]
variables that can only be accessed from within the method that they are defined.
- (void) reduce
int u = numerator;
You must set local variables to a value before you can use them.
The static keyword
You can have a local variable
retain its value through multiple invocations of a method by placing the
keyword static in front of the variable's declaration.
static int hitcount = 0;
Has an intial value of 0.