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:
- @protocol NSCopying
- -(id)copyWithZone:(NSZone *)zone;
- @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:
- @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:
- @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:
- @protocol Drawing
- -(void) paint;
- -(void) erase;
- @optional
- -(void) outline;
- @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:
- id currentObject;
- ……
- if([currentObject conformsToProtocol:@protocol(Drawing)]==YES)
- {……}
- 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:
- @protocol Drawing3D <Drawing>
A protocol can also be used for classification. For example:
- @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:
- @interface Square:NSObject
- {Rectangle *rect;}
- -(int) setSide:(int)S;
- -(int) side;
- -(int) area;
- -(int) perimeter;
- @end;
Summary: DetailsObjective-CMediumCategoryAndProtocolThe operation is complete. I hope this article will help you!