member variables and property descriptions for a class

Source: Internet
Author: User

0x 01 Attribute property of Class

The properties of the class are the properties declared through @property. property is a struct of type objc_property. The structure encapsulates information about attributes such as the name of the property, the type of the property, the read-write property, non-atomic/atomic attributes, and so on.

1), get the property list of a class method: Objc_export objc_property_t *class_copypropertylist (class cls, unsigned int *outcount) The entry parameter is a class object , an address that is used to count the number of attributes for an integer. The return value is an array of attribute lists with the Objc_property_t object as an element.

2), get the name of a single attribute: Objc_export const char *property_getname (objc_property_t property) is an attribute of the struct body, the return value is a const char* string.

3), gets all descriptions of a single attribute: Objc_export const char *property_getattributes (objc_property_t property) is also an attribute of the struct body, The return value is a const char* string that describes all the attributes of the property.

4), gets the value of a single attribute: Objc_export char *property_copyattributevalue (objc_property_t property, const char *attributename)

__osx_available_starting (__mac_10_7, __iphone_4_3) Gets the current value of a property.

5), gets the grouping value for all descriptions of an attribute: Objc_export objc_property_attribute_t *property_copyattributelist (objc_property_t property, unsigned int *outcount)

The entry parameter is the structure of the property and an address that is used to count the number of attribute attributes. The return value is an array of elements that are composed of objc_property_attribute_t type struct bodies.

For example, there is an object Studentmodel:

1 @interfaceStudentmodel:nsobject2 {3NSString *_ivarone;4Uitextview *_ivartwo;5Uitextfield *_ivarthree;6NSNumber *_ivarfour;7 }8 9@property (nonatomic, copy) NSString *MyName;Ten @property (nonatomic, assign) Nsinteger MyNumber; One@property (nonatomic, retain) NSString *MyAge; A@property (nonatomic, weak) NSString *MyFirstName; -@property (Copy,ReadOnly) Nsarray *Array; -@property (nonatomic, copy) Nsmutablearray *Mutablearray; the@property (nonatomic, strong) Nsdictionary *dict; -@property (Nonatomic, getter=isright) BOOL right; -@property (Nonatomic, Setter=setlastname:) NSString *Mylastname; -@property (nonatomic, strong) Nsmutabledictionary *mutalbedict; +@property (nonatomic, strong) Nscharacterset *Set; - @property (nonatomic, assign) Nsinteger Myinteger; +@property (nonatomic, assign)intmyInt; A@property (nonatomic, assign) unsignedintMyunsignint; at@property (nonatomic, assign)floatmyfloat; -@property (nonatomic, assign)DoubleMyDouble; -@property (nonatomic, assign)LongMyLong; - @property (nonatomic, assign) point MyPoint; - @property (nonatomic, assign) Rect Myrect; -  in @end

Use the following code to get a list of the properties of the object Studentmodel:

1UnsignedintOutcount;2objc_property_t *propertylist = Class_copypropertylist ([Studentmodelclass], &outcount);//get a list of class properties3      for(unsignedinti =0; i < Outcount;i + + )4     {5objc_property_t property =Propertylist[i];6         Const Char*propertyname = Property_getname (property);//gets the name of the property7         Const Char*propertydesc = Property_getattributes (property);//Get Property Overall description8         Const Char*propertyvalue = Property_copyattributevalue (property, PropertyName);//gets the value of the property9NSLog (@"the description of the \n\n%s property is:%s = = = Value:%s", propertyname,propertydesc,propertyvalue);Ten          OneUnsignedintAttcount; Aobjc_property_attribute_t *attlist = Property_copyattributelist (property, &attcount);//Gets the description list of the property, and the result is an array of type objc_property_attribute_t struct -          for(unsignedintj =0; J < Attcount; J + +) { -objc_property_attribute_t att =Attlist[j]; the             Const Char* Name =Att.name; -             Const Char* Value =Att.value; -NSLog (@"%s property name is%s value is:%s", propertyname,name,value); -         } +}

The objc_property_attribute_t structure is defined as follows:

Defines a property attribute

typedef struct {

const char *name; /**< the name of the attribute * *

const char *value; /**< the value of the attribute (usually empty) */

} objc_property_attribute_t;

The input to the code is probably like this (pick a mylastname attribute output as an example):

1The description of the Mylastname property is: T@"NSString", the &,n,ssetlastname:,v_mylastname = = value is: (NULL)2  .-Geneva- -  -: $:20.393fmdbtest[3656:2462138] The name of the Mylastname property is the T value:@"NSString"3  .-Geneva- -  -: $:20.393fmdbtest[3656:2462138] The name of the Mylastname property is &values are:4  .-Geneva- -  -: $:20.393fmdbtest[3656:2462138] The name of the Mylastname property is the N value:5  .-Geneva- -  -: $:20.393fmdbtest[3656:2462138] The name of the Mylastname property is the S value: Setlastname:6  .-Geneva- -  -: $:20.393fmdbtest[3656:2462138] The name of the Mylastname property is the V value: _mylastname

The specific description of the objc_property_attribute_t structure is summarized in the following points:

A, name T indicates that the type of the property is of the base object type and the base data type, and the base object type is named for the object type name such as Nsarray, NSString, Nsmutabledictionary, and so on; in the base type: BOOL is B, Nsinteger is q, int is I, unsigned int is I, float is F, double is D, Long is Q, point is {point=ss}, Rect is {rect=ssss}, and so on

b, Name C indicates that the property is copy, the property is Strong;w for the &, the property is weak, the null representation property is Assgin, and the value above is no value

c, atomic attribute null represents an atomic attribute; name N is a non-atomic attribute, and value is no value when it represents atomic and non-atomic attributes

D, name v indicates the name of the property when value is the underlined property name

E, name R represents the read-only attribute readonly, at which value has no value

F, name is G to set the Getter method at this time value is the user-defined getter method name, name is s to indicate that the user has set setter method, at this time value is set by the user setter method

0x 02, member variables for classes

The member variables of the class include global variables and the properties of the class.

1), gets the list of class member variables: Objc_export Ivar *class_copyivarlist (class cls, unsigned int *outcount) into the class name and the address of the number of member variables used to count the class, The return value is the Ivar struct as an array of member variables for the element.

2), gets the name of a single member variable: objc_export const char *ivar_getname (Ivar V)

3), gets the type of a single member variable: objc_export const char *ivar_gettypeencoding (Ivar V)

4), gets the memory offset of a single member variable in the class: Objc_export ptrdiff_t Ivar_getoffset (Ivar v)

The following code gets all the member variables in the Studentmodel class:

1UnsignedintIvarcount;2     //gets a list of class member variables, including the properties of the class3Ivar *ivarlist = Class_copyivarlist ([Studentmodelclass], &ivarcount);4      for(unsignedinti =0; i < Ivarcount; i + +) {5         //gets the name of the specified member variable6         Const Char*ivarname =Ivar_getname (Ivarlist[i]);7         //gets the value of the member variable's type type reference property_copyattributelist in name T8         Const Char*ivartypeencoding =ivar_gettypeencoding (Ivarlist[i]);9         //Gets the memory offset value of the member variable in the class objectTenptrdiff_t Ivaroffset =Ivar_getoffset (Ivarlist[i]); OneNSLog (@"the name of the Ivarlist[i] member variable is:%s, the type is:%s, offset is:%td", ivarname,ivartypeencoding,ivaroffset); A}

The output of the code:

1  .-Geneva- -  -: $:20.397fmdbtest[3656:2462138] Ivarlist[i] The name of the member variable is: _ivarone, the type is:@"NSString", the offset is:82  .-Geneva- -  -: $:20.397fmdbtest[3656:2462138] Ivarlist[i] The name of the member variable is: _ivartwo, the type is:@"Uitextview", the offset is: -3  .-Geneva- -  -: $:20.397fmdbtest[3656:2462138] Ivarlist[i] The name of the member variable is: _ivarthree, the type is:@"Uitextfield", the offset is: -4  .-Geneva- -  -: $:20.397fmdbtest[3656:2462138] Ivarlist[i] The name of the member variable is: _ivarfour, the type is:@"NSNumber", the offset is: +

The type of the member variable is the same as the property type in 0x01. Reference above can

member variables and property descriptions for a class

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.