Adam Bowker Adam Bowker - 3 months ago 14x
Objective-C Question

Objective-C No getter method error

I'm very new to Objective-C (and programming in general... I've only studied Python and a little bit of HTML). I am trying to learn from the tutorial here but I'm running into an error that I can't seem to get figured out.

In Fraction.m, under - (void)add:(Fraction *)newFraction I'm getting the error "No getter method for read from property" on the next two lines.

If anyone could help me out that would be great! Any suggestions are appreciated.




#import "Fraction.h"
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {

Fraction *aFraction = [[Fraction alloc] init];
Fraction *bFraction = [[Fraction alloc] init];

[aFraction setNumerator:1 overDenominator:2];
[bFraction setNumerator:1 overDenominator:3];

[aFraction display]; NSLog(@" + "); [bFraction display]; NSLog(@" = ");
[aFraction add:bFraction];
[aFraction display];

return 0;


#import <Foundation/Foundation.h>

@interface Fraction : NSObject {
NSInteger numerator;
NSInteger denominator;
- (void)setNumerator:(NSInteger)value;
- (void)setDenominator:(NSInteger)value;
- (void)display;
- (void)setNumerator:(NSInteger)num overDenominator:(NSInteger)denom;
- (void)add:(Fraction *)newFraction;



#import "Fraction.h"

@implementation Fraction
- (void)setNumerator:(NSInteger)value {
numerator = value;

- (void)setDenominator:(NSInteger)value {
denominator = value;

- (void)display {
NSString *numeratorString = [[NSString alloc] initWithFormat:@"%ld", (long)numerator];
NSString *denominatorString = [[NSString alloc] initWithFormat:@"%ld", (long)denominator];
NSLog(@"%@/%@", numeratorString, denominatorString);

- (void)setNumerator:(NSInteger)num overDenominator:(NSInteger)denom {
self.numerator = num;
self.denominator = denom;

- (void)add:(Fraction *)newFraction {
// a/b + c/d = ((a * d) + (b * c)) / (b * d)
self.numerator = self.numerator * newFraction.denominator + self.denominator * newFraction.numerator;
self.denominator = self.denominator * newFraction.denominator;



There are two possible solutions

Solution 1

Change all self.numerator to numerator and all self.denominator to denominator

Solution 2

Declare numerator and denominator as properties

@property(nonatomic) NSInteger numerator, denominator;

Now change all numerator to self.numerator and all denominator to self.denominator