Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
 }
}
like image 425
Manni Avatar asked Dec 08 '22 00:12

Manni


1 Answers

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:.

like image 175
kevboh Avatar answered Feb 09 '23 00:02

kevboh