IOS Runtime Explore (iii): Starting from runtime to understand the properties of OC

Source: Internet
Author: User

You know, the runtime is here.

Reprint please specify the source http://blog.csdn.net/u014205968/article/details/67639303

This article mainly explains the runtime related knowledge, from the principle to the practice, because contains the content to be divided into the following five articles to explain in detail, may choose the direction which needs to understand by oneself:

    • Starting from Runtime: Understanding object-oriented classes to process-oriented structures
    • Starting from Runtime: in-depth understanding of the OC message forwarding mechanism
    • Starting from Runtime: Understanding the properties of OC
    • Starting from Runtime: Practice category Add attribute and black Magic method swizzling
    • Starting from Runtime: in-depth weak implementation mechanism

This article is a series of articles in the third article from the runtime: Understand the properties of OC, mainly from runtime the start to explain the property underlying implementation of properties and related methods, because the previous blog has been explained property in detail the underlying implementation, so this article will not repeat, If you need to see the relevant article: iOS @property Explore (a): basic details of the main explanation property of the basic and the modifier, IOS @property Explore (ii): In-depth understanding of the article is mainly in-depth code understanding property of the underlying implementation, As the content of this article is greatly duplicated, this article will not repeat the above related content.

This article will explain some runtime of the relevant methods of manipulating properties.

First review the relevant code and property the two structures related to the underlying implementation:

//OC definition of a custom class @interface  Person: nsobject @property(nonatomic, copy)NSString* CJMNAME;@property(nonatomic,Assign) Nsuinteger Cjmage;@end @implementation   person@synthesizeCjmname = _cjmname;@synthesizeCjmage = _cjmage;@end//clang code for. CPPstruct_prop_t {Const Char*name;Const Char*attributes;};Static struct /*_prop_list_t*/{unsigned intEntsize;//sizeof (struct _prop_t)        unsigned intCount_of_properties;struct_prop_t prop_list[2];} _objc_$_prop_list_person __attribute__ (used, section ("__data,__objc_const"))) = {sizeof(_prop_t),2,        {{"Cjmname","[Email protected]\" nsstring\ ", C,n,v_cjmname"},        {"Cjmage","Tq,n,v_cjmage"}}};

As we can see from the above code, a @property property is described as a struct at the bottom, so how do we get the struct? Can be obtained by the following code:

intMainintargcConst Char* argv[]) {@autoreleasepool{person* p = [[Person alloc] init]; P.cjmname = @"Jiaming Chen"; UnsignedintPropertycount =0; objc_property_t *propertylist = class_copypropertylist ([pclass], &propertycount); for(inti =0; i < Propertycount; i++) {Const Char* Name = Property_getname (Propertylist[i]);Const Char* attributes = Property_getattributes (Propertylist[i]); NSLog (@'%s '%s ', name, attributes); }    }return 0;}

First look at objc_property_t what it is, in which objc/runtime.h you can find the relevant definition:

typedefstruct objc_property *objc_property_t;

It is a pointer to a struct, struct objc_property where the structure struct objc_property is actually the structure of the file in the previous article .cpp struct _prop_t , through the class_copyPropertyList method can get all the properties of the relevant class, the specific function is declared as follows:

/** * describes theProperties declared byAclass. * * @param cls theclassYou want toInspect. * @param outcount onreturn,contains  the length  of  theReturned array. * If \e Outcount is\c NULL, the length  is  notReturned. *  * @returnAn array ofPointers ofType \c objc_property_t describing theProperties * declared by  the class. Any properties declared bySuperclasses is notIncluded. * The arraycontains\c *outcount Pointers followed byA \c NULL Terminator. Must free theArray with\c free (). * * If \e CLS declares no properties,or\e CLS is\c Nil, returns \c NULL and\c *outcount is\c0.*/objc_export objc_property_t *class_copypropertylist (class cls, unsigned int *outcount) objc_available (10.5,2.0,9.0,1.0);

As you can see from the comments, the first parameter is the class object of the related class (if you have questions about the first two articles in this series), the second parameter is a pointer to the unsigned int specified number, which allows you property to get all the properties, property_getName and then property_getAttributesmethod Gets the value described by the property name and the attributes result of the output is as follows:

2017-03-27 09:59:20.914487OCTest[2467:460742]cjmNameT@"NSString",C,N,V_cjmName2017-03-27 09:59:20.915321 OCTest[2467:460742] cjmAge TQ,N,V_cjmAge

nameWell understood, after the attributes contrast is not difficult to find its law, interested readers can also set several different types, different modifiers to property look at the output.

Besides, there are a few ways to get a property that describes a struct, adds properties, substitutes properties, and so on, based on the property name.

/** * Returns A Property  withAgiven name  ofAgiven class. * * @param cls theclassYou want toInspect. * @paramnameThename  of  the  PropertyYou want toInspect. *  * @returnA pointer ofType \c objc_property_t describing the  Property,or* \c NULLif  the class does  notDeclare a Property  with  that name,  *or\c NULLif\e CLS is\c Nil. */objc_export objc_property_t Class_getproperty (class CLS, const char *name) Objc_available (10.5,2.0,9.0,1.0);/** * Adds A Property  toAclass. * * @param cls theclass  toModify. * @paramnameThename  of  the  Property. * @param attributes an array of  PropertyAttributes. * @param attributecount the Number  ofAttributesinch\e attributes. *  * @return\c YESif  the  Propertywas added successfully, otherwise \c NO * ( forExample the classalready has that  Property). */objc_export BOOL Class_addproperty (class CLS, const char *name, const objc_property_attribute_t *attributes, unsigned int attributecount) objc_available (10.7,4.3,9.0,1.0);/** * Replace A Property  ofAclass. * * @param cls theclass  toModify. * @paramnameThename  of  the  Property. * @param attributes an array of  PropertyAttributes. * @param attributecount the Number  ofAttributesinch\e attributes. */objc_export void Class_replaceproperty (class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributecount) objc_available (10.7,4.3,9.0,1.0);

To give a simple chestnut:

intMainintargcConst Char* argv[]) {@autoreleasepool{person* p = [[Person alloc] init]; P.cjmage = -; P.cjmname = @"Jiaming Chen"; UnsignedintPropertycount =0; objc_property_t *propertylist = class_copypropertylist ([pclass], &propertycount); for(inti =0; i < Propertycount; i++) {Const Char* Name = Property_getname (Propertylist[i]);Const Char* attributes = Property_getattributes (Propertylist[i]); NSLog (@'%s '%s ', name, attributes); } objc_property_attribute_t attributes = {"[Email protected]\" nsstring\ ", C,n,v_studentidentifier","",        }; Class_addproperty ([Pclass],"Studentidentifier", &attributes,1); objc_property_t property = Class_getproperty ([pclass],"Studentidentifier"); NSLog (@'%s '%s ', Property_getname, Property_getattributes (property)); }return 0;}

Through the above method can add a property, because I have limited practical development of the above methods, the actual examples can not be raised so much more detailed.

Note

Due to the limited level of the author, there are inevitably flaws, if there are problems please enlighten.

IOS Runtime Explore (iii): Starting from runtime to understand the properties of OC

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.