Delegate integrated in the class, or is it written separately in the. h file?

Source: Internet
Author: User

Ext.: Http://stackoverflow.com/questions/11382057/declaring-a-delegate-protocol

There definitely is subtle differences. If the protocol you is talking about isADelegateThat isUsed by one particularclass, forExample, Myspecialviewcontroller, and myspecialviewcontrollerdelegate, then you might very-like to keep the Declarati On both of thoseinchThe same header. If anotherclass  isGoing to implement that protocol, forexample, it's probably going to depend logically on the Myspecialviewcontroller class. So, you're not introducing any additional dependencies. But, there's Another significant reason (at least) to use protocols. You might is trying to decouple a bidirectional dependency between. Of course, the compiler doesn'T let's headers#importone another. But, evenifYou move oneclass's #import to the. m file, it's often a sign of a poor design to has a classes each fully aware of one another's complete API.One-to- decouple ThisRelationship a little isTo make oneclassAware of the other is only through a protocol and the other implements. Perhaps Parent owns and creates the childclass, and thus must#import "Child.h". But the child also needs to the Foo:bar:method on the Parent. Could make a fooprotocol:@protocolFooprotocol- (void) Foo: (int) arg1 bar: (BOOL) arg2;@end and theninchParent.h:@interfaceParent:somebaseclass<fooprotocol>{}which allows child to Do  This:@interfaceChild {} @property (assign)ID<FooProtocol>Foohandler;and use it[foohandler foo:1Bar:yes]; Which leaves the child with no direct dependency on the Parentclass(or Parent.h). But, Thisonly worksifYou keep the declaration of FooprotocolinchFooProtocol.h, notinchParent.h. Again,if  ThisFooprotocol is only ever used by child and then it would make sense to keep itinchChild.h, but probably notif  Thisprotocol was used by classes other than child.so, to summarize, keep your protocolsinchSeparate headersifYou want to preserve the maximum ability to separate interdependencies between your classes, or to encourage better Separa tioninchYour design.

# # Baidu Video iOS version delegate is written separately in an. h file.

Delegate integrated in the class, or is it written separately in the. h file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.