OBJECTIVE-C Runtime Five: protocols and classifications

Source: Internet
Author: User
Tags list of attributes

Classification in Objective-C allows us to extend a class by adding methods (but we cannot add new instance variables through category), and we can do it without accessing the code in the class.

The protocol in Objective-C is a ubiquitous interface definition method, that is, the interface is defined by @protocol in one class, and the interface is implemented in another class. This interface definition method has also become the "delegation" mode. @Protocol declares that you can For methods implemented by any other method class, the protocol simply defines an interface, and other classes are responsible for implementation.

In this chapter, we take a look at the runtime support for classification and protocol.

Basic data types

Category

Category is a pointer to a structure of the category, which is defined as follows:


typedef struct objc_category * Category;
 
struct objc_category {
    char * category_name OBJC2_UNAVAILABLE; // category name
    char * class_name OBJC2_UNAVAILABLE; // class name to which the class belongs
    struct objc_method_list * instance_methods OBJC2_UNAVAILABLE; // list of instance methods
    struct objc_method_list * class_methods OBJC2_UNAVAILABLE; // class method list
    struct objc_protocol_list * protocols OBJC2_UNAVAILABLE; // list of protocols implemented by classification
}
This structure mainly contains the instance methods and class methods defined by the category, where the instance_methods list is a subset of the method list in objc_class, and the class_methods list is a subset of the metaclass method list.

Protocol

Protocol is defined as follows:

1
typedef struct objc_object Protocol;
We can see that the Protocol is actually an object structure.

Operation function

Runtime does not provide operation functions for classification in the <objc / runtime.h> header file. Because the information in these classifications is contained in objc_class, we can obtain the classification information through the operation function for objc_class. As shown in the following example:


@interface RuntimeCategoryClass: NSObject
-(void) method1;
@end
 
@interface RuntimeCategoryClass (Category)
-(void) method2;
@end
 
@implementation RuntimeCategoryClass
 
-(void) method1 {
 
}
 
@end
 
@implementation RuntimeCategoryClass (Category)
 
-(void) method2 {
 
}
 
@end
 
#pragma mark-
 
NSLog (@ "Tests if the method list in objc_class contains methods in the category");
unsigned int outCount = 0;
Method * methodList = class_copyMethodList (RuntimeCategoryClass.class, & outCount);
 
for (int i = 0; i <outCount; i ++) {
    Method method = methodList [i];
 
    const char * name = sel_getName (method_getName (method));
 
    NSLog (@ "RuntimeCategoryClass ‘s method:% s", name);
 
    if (strcmp (name, sel_getName (@selector (method2)))) {
        NSLog (@ "Classification method method2 is in the method list of objc_class");
    }
}
The output is:


2014-11-08 10: 36: 39.213 [561: 151847] Test whether the method list in objc_class contains the method in the classification
2014-11-08 10: 36: 39.215 [561: 151847] RuntimeCategoryClass ’s method: method2
2014-11-08 10: 36: 39.215 [561: 151847] RuntimeCategoryClass ’s method: method1
2014-11-08 10: 36: 39.215 [561: 151847] The classification method method2 is in the method list of objc_class
For Protocol, runtime provides a series of functions to operate on them. These functions include:


// return the specified protocol
Protocol * objc_getProtocol (const char * name);
 
// Get an array of all protocols known at runtime
Protocol ** objc_copyProtocolList (unsigned int * outCount);
 
// Create a new protocol instance
Protocol * objc_allocateProtocol (const char * name);
 
// Register the newly created protocol in the runtime
void objc_registerProtocol (Protocol * proto);
 
// Add method to the protocol
void protocol_addMethodDescription (Protocol * proto, SEL name, const char * types, BOOL isRequiredMethod, BOOL isInstanceMethod);
 
// add a registered protocol to the protocol
void protocol_addProtocol (Protocol * proto, Protocol * addition);
 
// Add attributes to the protocol
void protocol_addProperty (Protocol * proto, const char * name, const objc_property_attribute_t * attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty);
 
// return the protocol name
const char * protocol_getName (Protocol * p);
 
// test if two protocols are equal
BOOL protocol_isEqual (Protocol * proto, Protocol * other);
 
// Get the method description array of the method specified in the protocol
struct objc_method_description * protocol_copyMethodDescriptionList (Protocol * p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int * outCount);
 
// Get the method description of the specified method in the protocol
struct objc_method_description protocol_getMethodDescription (Protocol * p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod);
 
// Get the list of attributes in the protocol
objc_property_t * protocol_copyPropertyList (Protocol * proto, unsigned int * outCount);
 
// Get the specified attributes of the protocol
objc_property_t protocol_getProperty (Protocol * proto, const char * name, BOOL isRequiredProperty, BOOL isInstanceProperty);
 
// Get the protocol used by the protocol
Protocol ** protocol_copyProtocolList (Protocol * proto, unsigned int * outCount);
 
// Check if the protocol uses another protocol
BOOL protocol_conformsToProtocol (Protocol * proto, Protocol * other);
The objc_getProtocol function needs to be noted that if only a protocol is declared, and this protocol is not implemented in any class, the function returns nil.

● objc_copyProtocolList function, the obtained array needs to be released using free

● objc_allocateProtocol function, if the protocol with the same name already exists, returns nil

● objc_registerProtocol function, after creating a new protocol, you must call this function to register the new protocol in runtime. After the agreement is registered, it can be used, but you can't modify it, that is, you can't add methods or agreements to the agreement after registration.

It should be emphasized that once the protocol is registered, it cannot be modified, that is, it is no longer possible to add methods to the protocol by calling protocol_addMethodDescription, protocol_addProtocol, and protocol_addProperty.

summary

Runtime does not provide too many functions to deal with classification. For protocols, we can dynamically create protocols, add methods, properties, and inherited protocols to them, and get this information dynamically at runtime.

Objective-C Runtime 5: Protocols and Classifications

label:

Original address: http://www.cnblogs.com/LJson/p/4200798.html

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.