Manni
Manni

Reputation: 11148

Objective-C: How to check if a protocol-object is a special class

This Java-Code works:

public void executeCommand(ICommand cmd) { // ICommand is an Interface
 if (cmd.getClass().equals(LoginCommand.class)){

 }
}

But this Objective-C-Code doesn't work:

- (void)executeCommand: (id<Command>)cmd { // Command is a Protocol
 if ([cmd isKindOfClass:[LoginCommand class]]) {
  // WARNING: '-conformsToProtocol:' not found in protocol
 }
}

Upvotes: 4

Views: 2782

Answers (1)

kevboh
kevboh

Reputation: 5245

When you declare your protocol, tell it to inherit from the NSObject protocol like this:

@protocol Command <NSObject>
...
@end

reference is here. NSObject is a base protocol that implements -conformsToProtocol:.

Upvotes: 15

Related Questions