Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fSHAD

macrumors member
Original poster
Nov 10, 2011
46
0
UAE
I am reading the book Programming in Objective-C 2.0 by Stephan G.Kochan 3rd edition I tried this code sample

PHP:
//
//  main.m
//  fractions
//
//  Created by XFactor on 11/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

//interface 

@interface Fraction : NSObject {
    int numerator;
    int denumerator;
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDeNumerator: (int) d;

@end



//implementation
@implementation Fraction

-(void) print{
    NSLog(@"%i/%i", numerator,denumerator);
}

-(void) setNumerator:(int)n{
    numerator = n;
}
-(void) setDeNumerator:(int)d {
    denumerator = d;
}

@end

// main program
int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Fraction *myFraction;
    
   [B] 
   //Create instance of a fraction
    myFraction[ = [Fraction alloc];
    myFraction = [Fraction init];
    [/B]

    //setFraction to 1/3
    [myFraction setNumerator:1];
    [myFraction setDeNumerator:3];
    
    // Display the fractions using print method
    NSLog(@"The value of my fraction is:");
    [myFraction print];
    [myFraction release];
    
    [pool drain];
    return 0;
}

When creating an instance I am getting an error
PHP:
2011-11-13 21:44:24.492 fractions[639:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[Fraction<0x100001218> init]: cannot init a class object.'
*** First throw call stack:

Then I changed the code to
PHP:
myFraction = [[Fraction alloc] init];

The sample code which was mentioned in the book didn't work but the
tweaked code works perfectly why ?

I am using XCODE 4.1 !
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,560
6,059
You have a stray bracket in the original code.

Code:
   //Create instance of a fraction 
    myFraction[color=RED][[/color] = [Fraction alloc]; 
    myFraction = [Fraction init];

Although even without the bracket, it still looks weird... I've always done:

Code:
myFraction = [[Fraction alloc] init];

I feel like the sample code you showed would leak... like, what happens to the Fraction originally alloced as being myFraction? myFraction suddenly changes to being something completely different? Maybe you miswrote the code and it should actually look like this:

Code:
myFraction = [Fraction alloc];
myFraction = [myFraction init];

I don't know if that would work though.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,748
8,421
A sea of green
I think you read it wrong, or you typed it in wrong. Because you also misread or mistyped this code:
Code:
@interface Fraction : NSObject {
    int numerator;
    int [COLOR="Red"]denumerator[/COLOR];
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) [COLOR="red"]setDeNumerator[/COLOR]: (int) d;

@end
Kochan's code doesn't use the word "denumerator". It uses the word "denominator".


The author has a forum for the book. I suggest spending some time there.
 
Last edited:

fSHAD

macrumors member
Original poster
Nov 10, 2011
46
0
UAE
omg sorry I typed wrong !

its
Code:
myFraction = [Fraction alloc];
myFraction = [myFraction init];

not

Code:
myFraction = [Fraction alloc];
myFraction = [Fraction init];

This means I am initializing the instance myFraction ! not the class Fraction !

sorry my BAD tx 4 pointing out !
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.