views:

38

answers:

1
@protocol Runnable

- (id<Runnable>) works; //this compiles fine

- (Runnable *) broke; // get a compile error saying Expected ')' before 'Runnable'

@end

I'm not sure I understand why xCode complains about the - (Runnable *) broke; line

+5  A: 

Protocols in Obj-C don't look syntactically like, say, "interfaces" in Java, where the syntax for interface pointers and subclass pointers are essentially the same.

The id<Runnable> is the idiomatic way that you say "an object that conforms to Runnable. An id is a reference to any type of object, and the < > notation expresses an explicit conformance to a given protocol for the purposes of type checking.

If you say Foo *, you're referring to an object of either type Foo or one of its subclasses.

This just happens to be the Obj-C syntax for this. With this syntax, the semantics are similar to what you'd get in, e.g. Java.

quixoto
It would be more accurate to say that the syntax is different. They function pretty much the same, it's just that Objective-C doesn't talk about objects that conform to a protocol as instances of that protocol.
Chuck
+1 accurate stateement
Matt Williamson
@Chuck. Good clarification. Answer tweaked.
quixoto