Explanation of Objective-C classification and protocol operations

Source: Internet
Author: User

Objective-CMediumCategoryAndProtocolOperations are the content of this article, mainly fromObjective-COfCategory,Protocol, Merging objects, and so on. The content of this article is described in detail. Let's look at the details first.

I. Classification

It provides a simple way to modularize the definition of a class into the composition and classification of related methods. Provides a simple way to extend existing class definitions without accessing source code or creating subclass.

Class Classification considerations:

1) Although the category can access the instance variables of the original class, it cannot add any of its own variables and add subclasses.

2) The classification can be overloaded to another method in the class to consider this practice poor ).

3) Unlike General interfaces, you do not need to implement all methods in classification.

4) The object/category name must be unique.

Ii. Agreement

The Protocol is a list of methods shared by multiple classes. The methods listed in the Protocol are not implemented by others. The Protocol provides a way to use a specified name to define a set of somewhat relevant methods.

Define the protocol: Use the @ protocol command, and then the protocol name, and declare some methods as in the processing interface section. End with @ end. For example:

 
 
  1. @protocol NSCopying  
  2.         -(id)copyWithZone:(NSZone *)zone;  
  3.         @end 

Through a pair of angle brackets on the @ interface line <……>) To inform the compiler that a protocol is being used. The name of this protocol is placed after the class name and its classification name. For example:

 
 
  1. @interface AddressBook:NSObject <NSCopying> 

If there are multiple protocols in the class, you only need to column them in angle brackets and separate them with commas. For example:

 
 
  1. @interface AddressBook:NSObject <NSCopying,NSCoding> 

A class follows a protocol, and its subclass also complies with the protocol.

If you want to implement some methods that inherit from your class, you can use the Protocol to define these methods. For example:

 
 
  1. @protocol Drawing  
  2.         -(void) paint;  
  3.         -(void) erase;  
  4.         @optional  
  5.         -(void) outline;  
  6.         @end 

The Protocol does not reference any class. It is non-class.

You can use conformsToProtocol to check whether an object complies with a certain protocol. For example:

 
 
  1. id currentObject;  
  2.         ……  
  3.         if([currentObject conformsToProtocol:@protocol(Drawing)]==YES)  
  4.         {……}  
  5.     id <Drawing> currentObject; 

CurrentObject will contain objects that comply with the Drawing protocol. "<>" Multiple protocols can be added, separated by commas.

Defining a protocol can extend the definition of an existing protocol. For example:

 
 
  1. @protocol Drawing3D <Drawing> 

A protocol can also be used for classification. For example:

 
 
  1. @interface Faction (Staff) <NSCoping,NSCoding> 

Informal protocols are actually a classification. Informal protocols are usually defined by the root. It is actually a group of methods under a name and can be implemented as part of the Protocol. Classes that declare informal protocols do not implement these methods, and subclass that chooses to implement these methods must re-declare these methods in its interface section, and at the same time implement one or more of these methods. If an object adopts a formal agreement, it must comply with all information in the agreement. If an object adopts an informal protocol, it may not need to adopt all the methods of this Protocol, depending on this protocol.

3. Merging objects

Another technique that involves and defines one or more objects of other classes. For example:

 
 
  1. @interface Square:NSObject  
  2.         {Rectangle *rect;}  
  3.         -(int) setSide:(int)S;  
  4.         -(int) side;  
  5.         -(int) area;  
  6.         -(int) perimeter;  
  7.         @end; 

Summary: DetailsObjective-CMediumCategoryAndProtocolThe operation is complete. I hope this article will help you!

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.