"Does not recognize selector", but it's there.

Hey guys. I'm a reasonably experienced Java developer who's just playing around with Objective-C just to get a feel for it. I've created a simple "List" class (code below), and it compiles just fine, but when I try to run it, I get a the following error: "objc: List: does not recognize selector -print"

I'm a CS major, so I know this means that ObjC can't find the "-print" method in List, but I can't seem to convince it that it's there. It also doesn't seem to be running the appropriate "new" and "init" commands (I don't get the output from printf() in either one of those methods)
Even more infuriating is that, when compiling on a RedHat machine, it runs just fine. I'm not sure what I'm missing.

Below is my code and the command I use to compile:

------

List.h:

#import <objc/Object.h>

@interface List : Object
{
int list\[100\];
int size;
}

- (int) addEntry: (int) x;
- print;
- free;
- resetSize;
- init;

+ new;

@end

------

List.m:

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

@implementation List

+ new
{
printf("in new\n");
self = \[super new\];
return self;
}

- init
{
size = 0;
printf("in init\n");
fflush(stdout);
return self;
}

- free
{
return \[super free\];
}

- (int) addEntry: (int) x
{
list[size] = x;
size++;
return size;
}

- print
{
int i;
for (i = 0; i < size; i++)
{
printf("%d->", list\[\i\]); //ignore the slash in front of the 'i'. It's there so the forum formatting doesn't italicize things.
}
printf("\.\n");
return self;
}

- resetSize
{
size = 0;
return self;
}

@end

------

main.m

#import <objc/Object.h>
#import "List.h"

int main()
{
id foo;

foo = \[List new\];

\[foo print\];
\[foo free\];
return 1;
}
-------------------
</code>

The command: gcc -Wall -lobjc main.m List.m

I'm compiling on an iMac Core 2 Duo running 10.4.10.
Any ideas why it might not be finding the selector?

iMac Core Duo 2GHz, Mac OS X (10.4.10)

Posted on Nov 9, 2007 6:27 PM

Reply
3 replies

Nov 10, 2007 3:15 AM in response to Crono Deus

Okay after correcting your code (adding - before instance method names) and testing it...
My terminal gave the conclusion that you were declaring a class that already existed... 😀
The class List is defined in libobjc and doesn't define any
-[List print]
print method.

However, I know that's just a test but watch out Objective-C conventions. The +new method shouldn't be redefined, in the super class it calls [[self alloc] init] so the only method you should redefine is -init. For -free, you don't redefine it if it does nothing more than calling the super method. And also, you don't need to declare the methods you redefine already declared in the super class, the compiler knows them already.

Nov 12, 2007 10:11 AM in response to PsychoH13

Ack! Drat. The "+" and "-" signs were there when I copied the code over. I guess the formatting dropped them as well, but thanks for the heads up.

You're very right; once I changed the name to MyList, everything worked as I expected it to.

Thanks for the heads up about the ObjC conventions, too. It's good to know the good habits before getting too far into this. 🙂.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

"Does not recognize selector", but it's there.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.