iOS Runtime runtime

Source: Internet
Author: User

1, run-time introduction

1. What is the runtime

the run-time mechanism uses the C + + developed, is a set of Apple open-source framework

OC is based on the language of the runtime development, OC in All classes are loaded when they are run.

2. OC language

oc is a full-dynamic language, OC Everything is based on Runtime


3. Using scenes

1.< /span> Runtime Get properties of class dynamically

Span class= "S3" >// main application: Dictionary to model frame Mjextension jsonmodel

2. use Association object add attributes for classification

3. take advantage of Interchange method methods for intercepting systems or other frames

myth: the lower the level of technology used, the more efficient the framework will be

4. Header Files

#import <objc/runtime.h>

2. Run-time application

1. Get information about the properties of a column at run time

1. Get a list of properties for a class


/** if you want to dynamically get the properties of a class, you need to use the Class_copyivarlist member variable Class_copypropertylist property Class_copymethodlist method to the runtime mechanism Class_ Copyprotocollist protocol *////Returns the property list of the Class + (Nsarray *) propertylist {//0. Determine if there is an associated object, and if so, directly return the/** parameter 1> the associated property k associated with the object 2> EY */nsarray *plist = Objc_getassociatedobject (self, Propertieskey), if (plist) {return plist;} 1. Gets the ' class's Properties '/** 1> class 2> property's count pointer */unsigned int count = 0;//return value is an array of all properties obj_property_objc_property_t *list = class_copy PropertyList ([Self class], &count);//Create Storage property array Nsmutablearray *arraym = [Nsmutablearray arraywithcapacity:count]; for (int i = 0; i < count; ++i) {//Get property objc_property_t pty = list[i];//Get property name const char *cname = Property_getname (pty) ; [Arraym addobject:[nsstring stringwithutf8string:cname];} Releases the property array free (list);//sets the associated object/** 1> The associated object 2> the key 3> property of the associated object 4> the holding method of the attribute Reatin, copy, assign */objc_setassoc Iatedobject (self, propertieskey, Arraym, objc_association_copy_nonatomic); return arraym.copy;}

Dictionary-to-model method

Dictionary to model Method + (Instancetype) objectwithdict: (nsdictionary *) Dict{id obj = [[Self alloc] init];//get property list Nsarray *properties = [Self propertylist];//traversal property array for (NSString *key in properties) {//Determine if the dictionary contains this keyif (Dict[key]! = nil) {//Use KVC to set property values [ obj Setvalue:dict[key] forkeypath:key];}} return obj;}

2. Returning a list of methods for a class

///Returns the method list of the class + (Nsarray *) methodlist {//0. Determine if there is a dependency nsarray *mlist = Objc_getassociatedobject (self, Methodskey); if (mlist) {return mlist;} unsigned int count = 0;//1. Get methods List Method *list = Class_copymethodlist ([self class], &count);//Storage method Array Nsmutablearray *arraym = [Nsmutablearray arraywithcapacity:count];for (int i = 0; i < count; ++i) {//Get methods method = list[i];//Get method name sel mname = Method_get Name (method); [Arraym Addobject:nsstringfromselector (Mname)];} Release array free (list);//Set Dependency objc_setassociatedobject (self, methodskey, Arraym, objc_association_copy_nonatomic);
  return arraym.copy;} 


3. Returns the list of implementation protocols for the class

Returns the implementation protocol list for the class + (Nsarray *) protocollist {//0. Determine if there is a dependency nsarray *plist = Objc_getassociatedobject (self, protocolkey); if (pList) {return pList;} unsigned int count = 0;//2. Get the Protocol list protocol * __unsafe_unretained *list = Class_copyprotocollist ([self class], &count);//Create Protocol array Nsmutablearray * Arraym = [Nsmutablearray arraywithcapacity:count];for (int i = 0; i < count; ++i) {//Get agreement protocol * __unsafe_unretaine D prot = list[i];const char *pname = protocol_getname (prot); [Arraym addobject:[nsstring stringwithutf8string:pname];} return Arraym;}

Instance Source Click Open link












iOS Runtime runtime

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.