interface 'NSObject' is not a successor of 'MyClass'

Hi All,

Just learning Objective C. AppCode is sweet.

I'm looking at an example of how to create a constructor from here:
http://www.roseindia.net/iphone/objectivec/constructors.shtml

When I use this I see a warning (code intention yellow bar) on this line:

    self = [super init];

which says interface 'NSObject' is not a successor of 'MyClass'.

Any idea what this means?  Is there something wrong with the sample code?

TIA

4 comments

Hi!

First of all, I'm not sure wether you choose an iPhone application or a Mac application as your project template. Next, I don't think this is an bug of something like that in AppCode.

Let's step through the RoseIndia example:

1) Create a new Cocoa Application project

2) Create a new class MyClass

3) Paste in the example code from the RoseIndia website

4) Put the main() example code in applicationDidFinishLoading method in your AppDelegate. Like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    // use constructor
        MyClass *class = [[MyClass alloc] set :10 andb :12];
        [class sum];
        [class release];
        return;
}



5) Add 'return nil' to the end of the set:(int)x-method in MyClass.m

6) Organize / optimize your imports (Ctrl Alt - O)

7) Build and run your application.

The result will be "Sum is: 22".

So, in short: your error was due to example code. Copy and paste wasn't sufficient in this case.

Best regards,

Rutger
0

Ok that's cool. I did all that. But MyClass still shows the warning "interface 'NSObject' is not a successor of 'MyClass'". I don't understand what this warning means.

#import<stdio.h>
#import"MyClass.h"

@implementation MyClass

// define constructor
- (MyClass *)set:(int)x andb:(int)y {
    self = [super init]; // warning appears for this line.
    if (self) {
        a = x;
        b = y;
        return self;
    }
    return nil;
}

- (void)sum {
    printf("Sum is : %d", a + b);
}
@end

0

I guess I found the the fix, change your line to:

self = (MyClass *) [super init];


Now it works. The compiler needs to know on which class it should perform the init (as far as I understand).

A full and detailled explanation is found at http://cocoawithlove.com/2009/04/what-does-it-mean-when-you-assign-super.html

Good luck!

0

Please sign in to leave a comment.