objective c - How to create categories in separate .h and .m files? -
in book i'm reading explain how create categories in main file, i'm trying figure out how do in separate files..
i want create category fraction class called "mathoperations", opened new .h , .m called fraction+mathoperations since know convention.
this 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; homecoming 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]; homecoming result; } -(fraction *)multiply:(fraction *)f { fraction *result = [[fraction alloc] init]; result.numerator = numerator * f.numerator; result.denumerator = denumerator * f.denumerator; //[result reduce]; homecoming result; } -(fraction *)divide:(fraction *)f { fraction *result = [[fraction alloc] init]; result.numerator = numerator * f.denumerator; result.denumerator = denumerator * f.numerator; //[result reduce]; homecoming result; } @end
obviously i'm declaring wrong cause im getting bunch of errors variables not declared..also, more reasonable declare new interface , implementation in original fraction class?
errors:
in fraction class have property called "numerator" , "denumerator", seem not declare in meth implementation file tnx
you code right except few errors.
find corrected , replace in file
// // fraction+mathoperations.m // prog3 // // created niroohayon on 18/02/13. // copyright (c) 2013 niroohayon . 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; homecoming 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]; homecoming result; } -(fraction *)multiply:(fraction *)f { fraction *result = [[fraction alloc] init]; result.numerator = self.numerator * f.numerator; result.denumerator = self.denumerator * f.denumerator; //[result reduce]; homecoming result; } -(fraction *)divide:(fraction *)f { fraction *result = [[fraction alloc] init]; result.numerator = self.numerator * f.denumerator; result.denumerator = self.denumerator * f.numerator; //[result reduce]; homecoming result; } @end
objective-c
No comments:
Post a Comment