So I've been reading the "Programming in Objective-C Fourth Edition" book and it doesn't really explain how Instances and Methods work. At least into a way I can understand it.
Here is an example it told me to type out from the book. Thing is I don't understand a few key features. Can anyone help?
Here is an example it told me to type out from the book. Thing is I don't understand a few key features. Can anyone help?
Code:
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
-(void) print;
-(void) setNumberator: (int) n; // <--- WHY DOES THERE NEED TO BE "n"?
-(void) setDenominator: (int) d; // <--- WHY DOES THERE NEED TO BE "d"?
@end
@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}
-(void) setNumerator:(int)n
{
numerator=n;
}
-(void) setDenominator:(int)d
{
denominator=d;
}
@end
int main (int argc, char * argv[])
{
@autoreleasepool {
Fraction *myFraction; // <-- What does this section do?
myFraction = [Fraction alloc]; // <--
myFraction = [myFraction init]; // <--
[myFraction setNumerator:1];
[myFraction setDenominator:3];
NSLog(@"The value of myFraction is:");
[myFraction print];
}
return 0;
}
Last edited: