Ju-v
Ju-v

Reputation: 362

Issue with categories in Objective-C

I'am doing some exercises, but can't understand what is wrong.

I have:

Fraction+MathOps.h

#import "Fraction.h"

@interface Fraction (MathOps)
-(Fraction *) add:(Fraction *) f;
@end

Here is Fraction+MathOps.m

#import "Fraction+MathOps.h"

@implementation Fraction (MathOps)

-(Fraction *) add:(Fraction *) f
{
    //To add two fraction
    // a / b + c / d = ((a * b) + (b * c)) / (b * d)

    Fraction *result = [[Fraction alloc] init];

    result.numerator = (self.numerator * f.denominator) + (self.denominator * f.numerator);
    result.denominator = self.denominator * f.numerator;

    [result reduce];

    return result;
}

@end

and will try to call method add from categories in main.m

Fraction *ca = [[Fraction alloc] init];
Fraction *cb = [[Fraction alloc] init];
Fraction *cresult;

[ca setTo: 1 over: 3];  
[cb setTo: 2 over: 5];  

cresult = [ca add: cb];

and have compiler error (No visible @interface for 'Fraction' declares the selector 'add:' ) at cresult = [ca add: cb] string.

Upvotes: 1

Views: 986

Answers (2)

russes
russes

Reputation: 1130

I just stumbled across this issue today, with Xcode 7.3. Using the example above, removing the first line from Fraction+MathOps.m caused Xcode to understand the additional (category) selector without throwing errors.

After removing the #import line from Fraction+MathOps.h, I put it back. No more issues.

If you see this message, a little creative text editing may cause Xcode to fix the problem for you.

Upvotes: 0

Ju-v
Ju-v

Reputation: 362

Problem solved: I didn't include Fraction+MathOps.h in main.m Thanks Richard and Carl Norum

Upvotes: 2

Related Questions