views:

773

answers:

5
@protocol Eating
@end

@interface Eat : NSObject<Eating>
{
}
- (id<Eating> *)me;
@end

@implementation Eat
- (id<Eating> *)me { return self; }
@end

In the above piece of Objective-C code, why does "return self" result in a "Return from incompatible pointer type" warning? What's the incompatible pointer type and how to fix it?

A: 

remove id * and replace with id. id is already a pointer.

CiNN
A: 

Okay .. answer is "id" instead of "id *".

me2
You should accept one of the answers that were given to you.
Jorge Israel Peña
Please accept an answer, so it gets highlighted on the site.
pgb
Why downvote? He posted this as the first one because he figured the problem out himself.
Georg
A: 

You are slightly off in your use - it's:

- (id<Eating>)me { return self; }

(because you are returning id, not a pointer to an object).

Kendall Helmstetter Gelner
What's the difference between **a pointer to an object** and **id**?
Georg
Actually there is none - which is why your code did not work. (NSObject *) and (id) are kind of already the same thing (not really but close enough for this discussion) so when you say (id *) it's like saying (NSObject **). Yes it's a little weird that id and Class are both objects where you don't use the *...
Kendall Helmstetter Gelner
+2  A: 

Because id is a pointer itself, you don't need the asterisk.

@interface Eat : NSObject<Eating> {
}
- (id<Eating>)me;
@end
Georg
It's an asterisk, not an Asterix: http://en.wikipedia.org/wiki/Asterix ;) (took me three times to get this comment right!)
dreamlax
+2  A: 

Because id is essentially NSObject * (although there are some minor differences). Thus, when you return self, you are returning -(NSObject *). What you have is id * which is like NSObject **.

jbrennan
`id` is a typedef for `struct objc_object*`, not `NSObject *`. The difference is significant.
dreamlax
OK.
jbrennan