Types and protocols in Objective-C

Source: Internet
Author: User

Category

Before talking about classification, We can first explore why there is a classification mechanism in OC. What are the benefits? Assume that you have received a large project: Calculate the sum and difference of two integers. When you receive the task, start it immediately. Write the following code:
#import <Foundation/Foundation>- () sumNumber1:()num1 andNumber2: ()num2;- () subNumber1:()num1 andNumber2: ()num2; - (
Now, complete the work. A few days later, the customer's requirements changed, not only the addition and subtraction functions, but also the multiplication and division functions. We generally add multiplication and division methods directly in the Calculate class. Of course there is no problem here. However, in real projects, when the amount of code is huge and the business logic is quite complex, directly modifying the source code is at great risk! Now we have another approach: classification.
#import <Foundation/Foundation> Calculate(MulAndDiv)- () MulNumber1:()num1 andNumber2: ()num2;- () DivNumber1:()num1 andNumber2: ()num2; 
In this way, the function is extended without modifying the original code! Note the following when using classification: 1. A category can access the member variables in the original class, but no variables can be added to the category. If you want to add a variable, you can consider creating a subclass. 2. The method name in the category can be the same as a method name in the original class (that is, overload), but the method with the same name in the original class will be invalid and cannot be accessed. 3. You can create many categories. 4. You do not need to implement all classification methods. You can declare all methods in the classification, but do not implement them. You can implement them as needed. 5. The method of adding categories can be inherited by the quilt class.

Protocol

It may be hard to understand the protocol for the first time. The protocol can also be understood as a rule or convention. This is like the law that we all must abide by. laws include Constitution, Criminal Law, and civil law .... It involves many aspects and tells us what to do under different circumstances. Laws are applicable. Chinese laws must be followed as long as they are Chinese citizens, but Americans can not. Similarly, Chinese people may not comply with American laws! The protocol in OC is equivalent to the law in life. It defines many methods, but it is not implemented! This requires the classes that comply with this Protocol to implement these methods. This is like paying taxes in compliance with the law, but the law itself does not pay taxes. The Protocol does not have a parent class and cannot define member variables (here is a line: Is there Wang fa? Wang fa? I am Wang Fa! Obviously, the law cannot become a person, and the law does not have the body and will not eat.) an agreement is a list of methods shared by multiple classes. methods listed in the agreement are not implemented accordingly, if a class uses a protocol named MyProtocol, you must implement the @ required (required) Identification Method in the MyProtocol. The Protocol can be implemented by @ optional! How to use the Protocol: @ interface Person: NSObject <chinaProtocol> This statement tells the compiler that the Person complies with the chinaProtocol protocol. If multiple protocols are used, they only need to be listed in angle brackets, separated by commas: @ interface Person: NSObject <chinaProtocol, englishProtocol> Summary of related Protocols: 1. if a class complies with a certain protocol, its subclass also complies with this Protocol 2. the Protocol is classless. Any class can comply with a certain protocol. you can add <protocol name> after the type name to enable the compiler to help check the consistency of variables. 4. if the objects stored in variables comply with multiple protocols, you can list multiple protocols. when defining a protocol, you can extend the existing protocol, that is, the new protocol also complies with the old Protocol @ proticol Drawing3D <Drawing> Drawing3D Protocol also uses the Drawing protocol, therefore, the class using the Drawing3D protocol must implement the Drawing3D listing method and the Drawing listing Method 6. the classification protocol @ interface Person (chinaPerson) <NSCopying, NSCoding> indicates that the Person class has a classification chinaPerson, which complies with the NSCopying and NSCoding protocols.

 

I understand the above. If there are any mistakes, please correct me!

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.