Amazon.com Widgets Program 11.1
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 28, 2014, 03:30:47 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 11
| | |-+ Program 11.1
Pages: [1] Go Down
Print
Author Topic: Program 11.1 (Read 2243 times)
iamawh
Newbie
*
Posts: 4


Email




on: January 14, 2012, 02:55:51 AM

The Program 11.1 example is written in a single file. I tried writing the .h and .m files separately for the category Fraction (MathOps). However, how can the synthesized numerator and denominator in Fraction.m be accessed for the category files? Thanks.
Logged
tknp
Newbie
*
Posts: 23






Reply #1 on: January 17, 2012, 04:04:38 PM

Using self.numerator self.denominator?

Here is my 11.1 broken into multiple files.

Fraction.h
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Fraction : NSObject

@property int numerator, denominator;

-(void)         print;
-(void)         setTo: (int) n over: (int) d;
-(double)       convertToNum;
-(void)         reduce;
@end

Fraction+MathOps.h
Code: (Objective-C)
#import "Fraction.h"

@interface Fraction (Mathops)

-(Fraction *) add: (Fraction *) f;
-(Fraction *) sub: (Fraction *) f;
-(Fraction *) mul: (Fraction *) f;
-(Fraction *) div: (Fraction *) f;
-(Fraction *) inv;
@end

Fraction+MathOps.m
Code: (Objective-C)
#import "Fraction+MathOps.h"

@implementation Fraction (MathOps)
-(Fraction *) add: (Fraction *) f
{
// To add two fractions
Fraction *result = [[Fraction alloc] init];

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

return result;
}

-(Fraction *) sub: (Fraction *) f
{
// To sub two fractions
Fraction *result = [[Fraction alloc] init];

result.numerator = (self.numerator * f.denominator) - (self.denominator * f.numerator);
result.denominator = self.denominator * f.denominator;
[result reduce];

return result;
}

-(Fraction *) mul: (Fraction *) f
{
// To mul two fractions
Fraction *result = [[Fraction alloc] init];

result.numerator = self.numerator * f.numerator;
result.denominator = self.denominator * f.denominator;
[result reduce];

return result;
}

-(Fraction *) div: (Fraction *) f
{
// To div two fractions
Fraction *result = [[Fraction alloc] init];

result.numerator = self.numerator * f.denominator;
result.denominator = self.denominator * f.numerator;
[result reduce];

return result;
}

-(Fraction *) inv
{
// To invert a fraction
Fraction *result = [[Fraction alloc] init];

result.numerator = self.denominator;
result.denominator = self.numerator;
[result reduce];

return result;
}
@end

main.m (old Xcode version)
Code: (Objective-C)
#import "Fraction.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

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

[a setTo: 1 over: 3];
[b setTo: 2 over: 5];

[a print]; NSLog(@"  +"); [b print]; NSLog(@"----");
result = [a add: b];
[result print];
NSLog (@"\n");

[a print]; NSLog(@"  -"); [b print]; NSLog(@"----");
result = [a sub: b];
[result print];
NSLog (@"\n");

[a print]; NSLog(@"  *"); [b print]; NSLog(@"----");
result = [a mul: b];
[result print];
NSLog (@"\n");

[a print]; NSLog(@"  /"); [b print]; NSLog(@"----");
result = [a div: b];
[result print];
NSLog (@"\n");

[b print]; NSLog(@"inverted"); NSLog(@"----");
result = [b inv];
[result print];
NSLog (@"\n");


    [pool drain];
    return 0;
}
Logged
dharr19
Full Member
***
Posts: 175


Email




Reply #2 on: February 05, 2012, 03:45:02 PM

Hi,

Did you use the Object-C category template or did you just create a header file called MathOps.h?

Thanks
David
Logged
ljaygould
Newbie
*
Posts: 16






Reply #3 on: February 13, 2012, 07:17:11 AM

Having (I think) the same problem.

I tried it two ways: the first, adding the @interface Fraction (MathOps) and @implementation Fraction (MathOps) to the existing fraction.h and fraction.m files which the author indicates is an option. I then went all the way, creating a category in Xcode (in other words File, New, Objective-C Category, category MathOps of class Fraction).

Copying the code from the existing files, all h*** breaks loose, with every mention of denominator and numerator (synthesized in Fraction.m with @property reference in Fraction.h, all worked well when these were not broken out) with angry red error marks. The compiler does not recognize numerator and denominator as having been declared. HELP!

2/18/12 update to this 2/13 question: The answer was given in a different post here, which is that when separating out the files the synthesized or otherwise declared ivars need to be prefaced with "self." I'm hoping Steven will add this requirement to subsequent printings of the book; the examples work perfectly well in the same h/m files as the class implementation, but fail without ivars numerator and denominator being stated as self.numerator and self.denominator throughout. Then the only caution is to be sure the correct file (the Category header - in this case, Fraction+MathOps.h, or a consolidated header file with all the class headers for Fraction) be #imported into main.m.
Last Edit: February 18, 2012, 09:37:03 AM by ljaygould Logged
urbanlung
Newbie
*
Posts: 13






Reply #4 on: May 15, 2012, 03:18:03 PM

Hi, Guys, I had similar problem, undeclared var re numerator and denominator when doing categories as separate files. Fixed it by putting "self." in front of offenders. Previous versions of Xcode seemed ok without "self." but current one needs 'em, at least as far as i can tell. Current Kindle code does not work for me as is (assuming i've not made a booboo).
Logged
rickyshands
Newbie
*
Posts: 8






Reply #5 on: June 06, 2012, 10:08:41 AM

@tjnp, ljaygould

Thanks.  Thank you both.
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.