Resolved [Basic Question] Problem with init !

Discussion in 'iOS Programming' started by fSHAD, Nov 13, 2011.

  1. fSHAD, Nov 13, 2011
    Last edited: Nov 13, 2011

    fSHAD macrumors member

    Joined:
    Nov 10, 2011
    Location:
    UAE
    #1
    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;
    -(
    voidsetNumerator: (int) n;
    -(
    voidsetDeNumerator: (int) d;

    @
    end



    //implementation
    @implementation Fraction

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

    -(
    voidsetNumerator:(int)n{
        
    numerator n;
    }
    -(
    voidsetDeNumerator:(int){
        
    denumerator d;
    }

    @
    end

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

        
    NSAutoreleasePool pool = [[NSAutoreleasePool allocinit];
        
    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 allocinit];
    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 !
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    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.
     
  3. chown33, Nov 13, 2011
    Last edited: Nov 13, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    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.
     
  4. fSHAD thread starter macrumors member

    Joined:
    Nov 10, 2011
    Location:
    UAE
    #4
    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 !
     

Share This Page