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

Dark Matter

macrumors newbie
Original poster
May 5, 2010
4
0
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:

[Session started at 2010-05-05 20:58:37 +0100.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1461.2) (Fri Mar 5 04:43:10 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
Loading program into debugger…
Program loaded.
run
[Switching to process 31009]
Running…
2010-05-05 20:58:38.172 prog2[31009:a0f] *** NSInvocation: warning: object 0x100002080 of class 'Fraction' does not implement methodSignatureForSelector: -- trouble ahead
2010-05-05 20:58:38.175 prog2[31009:a0f] *** NSInvocation: warning: object 0x100002080 of class 'Fraction' does not implement doesNotRecognizeSelector: -- abort
sharedlibrary apply-load-rules all
(gdb)


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?
 
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...
 
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...

I was just following what the book told me. I tried release instead, but it didn't seem to fix the problem...
 
What is the exact title and author of the book?

Which edition of the book?

I could probably guess, but accuracy is important here.
 
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.
 
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
 
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.
 
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
 
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.
 
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];
 
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.
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.