views:

43

answers:

1

I'm learning ObjectiveC with "Programming in Objective C" by Stephen G Kochan and I'm having some difficulties with an example. It creates a simple Fraction class that inherits from Object. That's where I get into trouble, when I try to send messages that are understood by Object instead of Fraction, such as init, alloc or free (see code below):

// Fraction
#import <stdio.h>
#import <objc/Object.h> // base object

// @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 Instance of fraction 
    myFraction = [Fraction alloc];
    myFraction = [Fraction init];

    // Set fraction to 1/3
    [myFraction setNumerator: 1];
    [myFraction setDenominator: 3];


    // Display the fraction
    printf("The value of my fraction is: ");
    [myFraction print];
    printf("\n");


    // Destroy the instance
    [myFraction free];


    return 0;
}

When I compile it: gcc fraction.m -o fraction -l objc

I get the following warnings:

fraction.m: In function ‘main’:
fraction.m:47: warning: ‘Fraction’ may not respond to ‘+alloc’
fraction.m:47: warning: (Messages without a matching method signature
fraction.m:47: warning: will be assumed to return ‘id’ and accept
fraction.m:47: warning: ‘...’ as arguments.)
fraction.m:48: warning: ‘Fraction’ may not respond to ‘+init’
fraction.m:62: warning: ‘Fraction’ may not respond to ‘-free’
fernando@McFofo ~/code/learning objective c:: ./fraction 
objc[1678]: Fraction: Does not recognize selector forward::

and the program, when run, complains about an illegal instruction…

Anybody knows what's going on?

+1  A: 

Classes in Cocoa (the most popular Objective-C library, they are almost synonymous) tend to inherit from NSObject, the Cocoa root class. It is documented here.

There is also the NSObject protocol, implemented by NSProxy. It forms the basis of Cocoa's support for Distributed Objects. That said, we may be getting ahead of ourselves with that!

I also notice that you are trying to do the following:

Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [Fraction init];

You are allocating memory for your object and then losing it by overwriting the pointer with another one. The standard practice is to do it on one line as follows:

Fraction *myFraction = [[Fraction alloc] init];

If you must do it one multiple lines, you must do the following:

Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [myFraction init];
// myFraction is now ready to go!

I stumbled across a site that refers to what appears to be the book you're reading. The examples highlighted on the website inherit from NSObject and import <Foundation/NSObject.h>. Is there any particular reason you eschewed this and decided upon the Object class provided by the runtime?

Sedate Alien
I just found out what I was doing wrong. The book I have is for Objective C 1.0 instead of 2.0. At least I know why it cost me only 2 bucks… :-P
Fernando
Fernando: Yup. As expressed in the Object.h header through preprocessor guards, all the good stuff perished from the Object class in Objective-C 2.0. Nowadays, you would have to invoke runtime library magicks to instantiate Object; your time is better spent learning Objective-C through the Cocoa or Cocoa Touch framework. On another note: Don't forget that an `@implementation` line does not need and should not have a semicolon at the end of it.
Peter Hosey
Peter, if I understand correctly, there's 2 different and parallel class libraries, the one descending from Object and the one descending from NSObject. Is this correct? If yes, why this duplicity? Why not just extend the original class library?
Fernando
Fernando: Object was from libobjc, NSObject is from Foundation (part of Cocoa and Cocoa Touch). Strictly speaking, it is possible to write an Objective-C program without Cocoa or Cocoa Touch; it's just a lot harder since you're foregoing all the batteries that Cocoa and Cocoa Touch include, so you have to make your own. As for why Foundation's NSObject doesn't descend from Object: The pre-1.0 Object had C-style memory-management rules, whereas NSObject has reference-counting (and now GC). Nowadays, they *could* make NSObject descend from Object, but there's no benefit from it.
Peter Hosey