21

What's the ObjectiveC syntax for specifying a protocol as an argument in a method?

Say I have 2 protocols, MyProtocol and MyProtocolCB:

@protocol MyProtocolCB <NSObject>
- (void) func;
@end

@protocol MyProtocol <NSObject>
- (void) register:(MyProtocolCB*) cb;
@end

I'm receiving this syntax error: error: expected type-specifier before 'MyProtocolCB'

2 Answers 2

51

Try:

- (void) register:(NSObject<MyProtocol>*) cb;
Sign up to request clarification or add additional context in comments.

2 Comments

You also might use more generic id<MyProtocol> instead of NSObject<MyProtocol>*, especially if MyProtocol already extends NSObject protocol
@iPhone beginner: it's not exactly the same. some methods in the NSObject class are not in the NSObject protocol
0

use id instead of NSObject as you already know id is the instance type or generic type so if you use id it will help you in big perspective.

- (void) register:(id<MyProtocol>*) sender;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.