开发者

what to do in a situation when a class doesn’t implement a method from a protocol [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this开发者_开发百科 question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜