IOS development,

Source: Internet
Author: User

IOS development,

Because Object-C does not support multi-inheritance, it is often replaced by Protocol. Protocol can define only one set of public interfaces, but it cannot provide specific implementation methods. That is to say, it only tells you what to do, but does not care about how to do it.

When a class uses a Protocol (Protocol), it must comply with the Protocol. For example, if you do not implement some necessary implementation methods, the compiler will report a warning to remind you that you have not followed the ×× protocol. Note: Here I am talking about warnings, not errors. Yes, even if you don't implement the "necessary implementation" methods, the program can run, but there are some additional warnings.

Role of Protocol:

1. define a set of Public interfaces (Public)

  • @ Required: required Method

  • @ Optional: optional implementation method (not all can be implemented)

2. Delegate proxy (Delegate) to pass the value:

It is a design pattern, which means entrusting others to do something.

Conclusion: an agreement is a rule or rule that is jointly observed; a proxy is a real-time entity that follows a specific agreement;

The following cited the blog in the author seeking truth to find part of the article, the feeling is clear: https://www.cnblogs.com/iyou/p/4939823.html

DaChu. h/*** The following is a fixed protocol declaration format. DaChuDelegate is the protocol name. Because it is a proxy protocol, the name format is: class Name + Delegate */@ protocol DaChuDelegate <NSObject>-(void) doSomethingAftercaiZuohaole; @ end @ interface DaChu: NSObject/*** delegate is an attribute of the dachu class, the weak keyword is used to avoid circular references. <DaChuDelegate> indicates that the DaChuDelegate protocol is followed * more straightforward: In the chef's mind, there is a person who accepts the good food to do something, the chef does not care about who this person is. The code of this person is delegate */@ property (nonatomic, weak) id <DaChuDelegate> delegate;-(void) kaiShiZuoCai; @ end
Dachu. m # import "DaChu. h "@ implementation DaChu-(void) kaiShiZuoCai {NSLog (@" start cooking "); sleep (2); NSLog (@" "); // The following sentence is used to determine whether delegate implements the doSomethingAftercaiZuohaole method. If delegate is not implemented, // directly [self. delegate doSomethingAftercaiZuohaole]; crash if ([self. delegate respondsToSelector: @ selector (doSomethingAftercaiZuohaole)]) {[self. delegate doSomethingAftercaiZuohaole] ;}}@ end

Let's take a look at the content in the proxy (Real-Time) class:

# Import "LaoWang. h "# import" DaChu. h "/** <DaChuDelegate> indicates compliance with the DaChuDelegate protocol and implementation of the methods in the Protocol */@ interface LaoWang () <DaChuDelegate> @ end @ implementation LaoWang-(void) laoWangKaiYe {NSLog (@ "Old Wang opened"); DaChu * dachu1 = [[DaChu alloc] init]; dachu1.delegate = self; // It indicates that Old Wang acts as a proxy, responsible for receiving food signals. [Dachu1 kaiShiZuoCai]; // The chef starts cooking}-(void) doSomethingAftercaiZuohaole {NSLog (@ "Old Wang knows"); // You can notify the waiter to go to serving food here} @ end

 

Summary: The Agency is similar to the Agency in real life, and the specific work agency helps you do it. The agreement is similar to the contract you signed with the Agency.

 

Related Article

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.