How do I provide a default implementation for an Objective-C protocol? -


i'd specify objective-c protocol optional routine. when routine not implemented class conforming protocol i'd use default implementation in place. there place in protocol can define default implementation? if not, best practice reduce copying , pasting default implementation on place?

objective-c protocols have no affordance default implementations. purely collections of method declarations can implemented other classes. standard practice in objective-c test object @ runtime see if responds given selector before calling method on it, using -[nsobject respondstoselector:]. if e object not respond given selector, method isn't called.

one way achieve result you're looking define method encapsulating default behavior you're looking in calling class, , call method if object doesn't pass test.

another approach make method required in protocol, , provide default implementations in superclasses of classes wherein may not want provide specific implementation.

there other options well, speaking there isn't particular standard practice in objective-c, except perhaps not call given method if hasn't been implement object, per first paragraph, above.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -