Objective-C Using Objects Error

Discussion in 'Mac Programming' started by Dark Matter, May 5, 2010.

  1. Dark Matter macrumors newbie

    Joined:
    May 5, 2010
    #1
    I've just started learning objective-c, from a book. I'm using Xcode to write it in. I've written in a piece of code that it told me to, but when I run it it displays this error message:


    This is the code I have:

    Code:
    // Program to work with fractions – class version
    
    #import <stdio.h>
    #import <objc/Object.h>
    
    //------- @interface section -------
    
    @interface Fraction: Object
    {
    	int numerator;
    	int denominator;
    }
    
    -(void)  print;
    -(void)  setNumerator: (int) n;
    -(void)  setDenominator: (int) d;
    
    @end
    
    //------- @implementation section -------
    
    
    @implementation Fraction;
    -(void) print
    {
    	printf (" %i/%i ", numerator, denominator);
    }
    
    -(void) setNumerator: (int) n
    {
    	numerator = n;
    }
    
    -(void) setDenominator: (int) d
    {
    	denominator = d;
    }
    
    @end
    
    //------- program section -------
    
    
    int main (int argc, char *argv[])
    {
    	Fraction *myFraction;
    	
    	// Create an instance of a Fraction
    	
    	myFraction = [Fraction alloc];
    	myFraction = [myFraction init];
    	
    	// Set fraction to 1/3
    	
    	[myFraction setNumerator: 1];
    	[myFraction setDenominator: 3];
    	
    	// Display the fraction using the print method
    	
    	printf ("The value of myFraction is:");
    	[myFraction print];
    	printf ("\n");
    	[myFraction free];
    	
    	return 0;
    }

    The only thing I could think of that may be the problem is the book was teaching objective-c 1.0, instead of 2.0, which I presume is what Xcode is using.

    Could you please help?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The only thing that jumps out at me is the free method call. Why are you calling free? This is not the Objective-C standard. Try release instead...
     
  3. Dark Matter thread starter macrumors newbie

    Joined:
    May 5, 2010
    #3
    I was just following what the book told me. I tried release instead, but it didn't seem to fix the problem...
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Well, exactly which line is crashing? The debugger in XCode will tell you...
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    What is the exact title and author of the book?

    Which edition of the book?

    I could probably guess, but accuracy is important here.
     
  6. Thomas Harte macrumors 6502

    Joined:
    Nov 30, 2005
    #6
    I think your Objective-C book must relate to one of the language variants outside of the scope of influence of NextStep and Apple?

    On the Mac, the object that all others descend from is NSObject, not Object. As previously implied, Cocoa implements a reference counted memory system which means you 'release', not 'free'. Releasing may or may not logically free, depending on whether anyone else has retained the object.

    Cocoa code also tends to be bound up with the idea of an autorelease pool and a run loop. The latter means that you pretty much never touch the 'main' function in a Cocoa program, the former means that you may get some memory leaks and related warnings on the console the way you're working. If you're just learning then I think there's an argument for not worrying about it for now.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    Probably the first edition of Kochan's "Programming in Objective-C". I believe this edition used Object as the base class, but it would be best to just use NSObject now. The second edition made this change.

    -Lee
     
  8. Dark Matter thread starter macrumors newbie

    Joined:
    May 5, 2010
    #8
    The book seems to be mainly for Macs, as the person writing it is using something called Project Builder, which I think is similar to Xcode, just older.

    The book is called "Programming in Objective-C" by Stephen Kochan.

    It is quite old (published in 2004) so it might be that things have moved on quite a lot since he wrote it, but I'm really not sure.
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    See my reply immediately before your most recent comment. It may be of value to get the newer edition, but at least for this particular issue changing from Object to NSObject should do it.

    -Lee
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    Nice one. You start the trouble by trying to invoke a method [myFraction free]. Maybe you meant [myFraction release]. Anyway, the free method is not very. When that happens, the Objective-C runtime system tries to find someone else to handle the method. So it uses methodSignatureForSelector to find which method you tried to call, and then doesNotRecognizeSelector to give the object a chance to handle it. These are both NSObject methods - not implemented in Object. When it finds that methodSignatureForSelector is not there, the Objective-C runtime gives you a warning "trouble ahead" (words of wisdom :rolleyes: ) and when doesNotRecognizeSelector is not found, the runtime aborts the program.

    BTW. I guess you didn't turn warnings on in XCode, or you ignored them.

    PS. Using release wouldn't change anything, because class Object doesn't recognize "release" either.

    PS. How did you install Objective-C on your computer? I can't find <objc/Object.h> anywhere.
     
  11. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    Ok I think we have all figured out the various things wrong with this.

    OP, presuming you have everything setup properly, copy/paste this over your original code and ensure it compiles and runs fine.

    Code:
    #import <Foundation/Foundation.h>
    
    //------- @interface section -------
    @interface Fraction: NSObject {
    	int numerator;
    	int denominator;
    }
    
    -(void)  print;
    -(void)  setNumerator: (int) n;
    -(void)  setDenominator: (int) d;
    
    @end
    
    //------- @implementation section -------
    @implementation Fraction;
    -(void) print {
    	printf (" %i/%i ", numerator, denominator);
    }
    
    -(void) setNumerator: (int) n {
    	numerator = n;
    }
    
    -(void) setDenominator: (int) d {
    	denominator = d;
    }
    @end
    
    //------- program section -------
    int main (int argc, char *argv[])
    {
    	Fraction *myFraction;
    	
    	// Create an instance of a Fraction
    	myFraction = [Fraction alloc];
    	myFraction = [myFraction init];
    	
    	// Set fraction to 1/3
    	[myFraction setNumerator: 1];
    	[myFraction setDenominator: 3];
    	
    	// Display the fraction using the print method
    	printf ("The value of myFraction is:");
    	[myFraction print];
    	printf ("\n");
    	[myFraction release];
    	
    	return 0;
    }
    
    Took out some whitespace to format better for webpage.
    Only #importing Foundation
    @interface Fraction: NSObject
    ...
    [myFraction release];
     
  12. Dark Matter thread starter macrumors newbie

    Joined:
    May 5, 2010
    #12
    Thanks everyone for helping me out. It works perfectly now :D
    I think I'll have to get the newer version of the book, so I won't get problems like this in the future.
     
  13. skochan macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #13
    You can also post book-specific questions (including from the first edition) to the book's forum at classroomM.com/objective-C.

    Good luck, :)

    Steve Kochan
     

Share This Page