Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Protocol Madness -- how to return object based on protocol?

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

like image 498
me2 Avatar asked Sep 06 '09 21:09

me2


1 Answers

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

@interface Eat : NSObject<Eating> {
}
- (id<Eating>)me;
@end
like image 166
Georg Schölly Avatar answered Nov 15 '22 12:11

Georg Schölly