what to do in a situation when a class doesn’t implement a method from a protocol [closed]
what to do in a situation when a class doesn’t implement a method from a protocol
Don't try to invoke that method on objects of the class.
Obviously, unless the protocol declares the method as optional, you must implement the method in all classes that conform to the protocol. That is the whole point of having the protocol at all.
However, if the method is optional, whenever you use it you must test the receiver to make sure it responds to the method.
e.g.
-(void) someMethod: (id<SomeProtocol>) foo
{
if ([foo respondsTo: @selector(optionalMethodInSomeProtocol)])
{
[foo optionalMethodInSomeProtocol];
}
}
If its an optional method you can leave it away, otherwise the class must implement all methods of the protocol. That's the rule.
精彩评论