Vi Matviichuk Vi Matviichuk - 4 years ago 145
Objective-C Question

Objective C: incompatible pointer types sending

I created a protocol, called


@protocol EventListener

I also have class,
, like:

// Processor.m
@synthesize listeners = _listeners; //@property NSMutableArray* in Processor.h

- (id) init {
self = [super init];
self.listeners = [[NSMutableArray alloc] init]; //init with empty array
return self;

- (void) addListener:(id<EventListener>)listener {
[self.listeners addObject:listener];

I have another class, called

@interface Plugin : NSObject <EventListener> { ... }

and in its implementation, I perform:

// Plugin.m
[self.processor addListener:self];

and get warning that

incompatible pointer types sending
sending to parameter of type

What I've done wrong?

I'm coming from java world and assume that because my Processor implements protocol it should be type of Listener, smith like:

interface Listener {

class Processor {
List<Listener> listeners = new ArrayList<Listener>();

void addListener(Listener listener) {

class Plugin implements Listener {
Processor processor = new Processor();

void method() {

Answer Source

In Objective-C, classes and protocols are completely separate things and — unlike Java's classes and interfaces — have distinct syntax.

If you wanted to accept any class which is, or is a descendant of, the class Listener then the parameter would be:

Listener *

If you wanted to accept any class that implements the protocol EventListener then the parameter would be:

id <EventListener>

As id means "any class type" and the <EventListener> adds that whatever is passed should implement that protocol.

If you wanted to accept only those of Listener and its subclasses that implement EventListener then the parameter would be:

Listener <EventListener> *

That contrasts with Java where you'd take the interface type directly and not specify any class type constraints.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download