MNY
MNY

Reputation: 1536

How to create categories in separate .h and .m files?

In the book that I'm reading he explain how to create the categories in the main file, but I'm trying to figure out how do I do this in a separate files..

I want to create a category for Fraction class called "MathOperations", so I opened a new .h and .m called Fraction+MathOperations since I know this is the convention.

This is Fraction.h

#import "Fraction.h" 

@interface Fraction : NSObject

@property int numerator, denumerator;

-(void) print: (BOOL) test;
-(void) setTo: (int) n over: (int) d;
-(void) reduce;
-(double) convertToNum;
-(id) addFrac:(id)f;
-(void) justPrint;
-(id) initWith:(int)n over:(int)f;
+(Fraction *) allocF;
+(int) count;
+(int) gCounter;

@end

Fraction+MathOperations.h

#import "Fraction.h"

@interface Fraction (MathOperations)

-(Fraction *) add: (Fraction *) f;
-(Fraction *) substract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;

@end

Fraction+MathOperations.m

    #import "Fraction.h"

@implementation Fraction (MathOperations)

-(Fraction *)add:(Fraction *)f
{
    extern int gCounter;
    Fraction *result = [[Fraction alloc] init];

    result.numerator = numerator * f.denumerator + denumerator * f.numerator;
    result.denumerator = denumerator * f.denumerator;
    [result reduce];
    ++gCounter;
    return result;

}


-(Fraction *)substract:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];

    result.numerator = numerator * f.denumerator - denumerator * f.numerator;
    result.denumerator = denumerator * f.denumerator;
    //[result reduce];
    return result;
}


-(Fraction *)multiply:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];

    result.numerator = numerator * f.numerator;
    result.denumerator = denumerator * f.denumerator;
    //[result reduce];
    return result;
}

-(Fraction *)divide:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];

    result.numerator = numerator * f.denumerator;
    result.denumerator = denumerator * f.numerator;
    //[result reduce];
    return result;
}

@end

Obviously I'm declaring something wrong cause im getting bunch of errors that variables are not declared..also, is it more reasonable to declare the new interface and implementation in the original Fraction class?

errors:

In the Fraction class I have some property called "numerator" and "denumerator", they seem not to be declare in the meth implementation file tnx

Upvotes: 1

Views: 122

Answers (3)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

You code is almost correct except few errors.

Find the corrected as and replace in your file

//
//  Fraction+MathOperations.m
//  prog3
//
//  Created by niroohayon  on 18/02/13.
//  Copyright (c) 2013 niroohayon . All rights reserved.
//

#import "Fraction.h"

@implementation Fraction (MathOperations)

-(Fraction *)add:(Fraction *)f
{
    //extern int gCounter;
    Fraction *result = [[Fraction alloc] init];

    result.numerator = self.numerator * f.denumerator + self.denumerator * f.numerator;
    result.denumerator = self.denumerator * f.denumerator;
    [result reduce];
   // ++gCounter;
    return result;

}


-(Fraction *)substract:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];

    result.numerator = self.numerator * f.denumerator - self.denumerator * f.numerator;
    result.denumerator = self.denumerator * f.denumerator;
    //[result reduce];
    return result;
}


-(Fraction *)multiply:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];

    result.numerator = self.numerator * f.numerator;
    result.denumerator = self.denumerator * f.denumerator;
    //[result reduce];
    return result;
}

-(Fraction *)divide:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];

    result.numerator = self.numerator * f.denumerator;
    result.denumerator = self.denumerator * f.numerator;
    //[result reduce];
    return result;
}

@end

Upvotes: 1

coverback
coverback

Reputation: 4413

You should use self. prefix, like self.numerator for each of the main class's property. Category doesn't have any "internal" access to the properties, it behaves like any external user.

Upvotes: 3

trojanfoe
trojanfoe

Reputation: 122391

I think simply changing this:

#import "Fraction.h"

to this:

#import "Fraction+MathOperations.h"

in Fraction+MathOperations.m will do the trick.

Upvotes: 1

Related Questions