In the recent period of time in the study of OC's run-time mechanism, encountered a called property_getattributes function, and then the use of a simple summary.
Property_getattributes is primarily used to obtain property data for a class, also known as metadata (metadata), which involves run-time functions including class_copypropertylist, Property_getname and Propert_getattributes
The general usage is as follows:
#import <objc/runtime.h>......-(void) custom{unsigned pcount; objc_property_t *properties=class_copypropertylist ([self class], &pcount);//property array for (int i=0;i<pcount;++i) { objc_property_t Property=properties[i]; NSLog (@ "propertyname:%s", Property_getname (property)); NSLog (@ "propertyattributes:%s", property_getattributes (property)); }}
The specific usage is as follows:
eg. defines a class Customclass, with attributes defined as follows
Header file:
Customclass.h#import <objc/runtime.h> @property (nonatomic, strong) NSString *myname;
Implementation file:
Customclass.m@synthesize myname;-(void) printallattributes{unsigned pcount; objc_property_t *properties=class_copypropertylist ([self class], &pcount);//property array for (int i=0;i<pcount;++i) { objc_property_t Property=properties[i]; NSLog (@ "propertyname:%s", Property_getname (property)); NSLog (@ "propertyattributes:%s", property_getattributes (property)); }}
The final output results are as follows:
2015-08-12 12:56:45.147 uimenucontroller[1924:146558] Propertyname:myname
2015-08-12 12:56:45.147 uimenucontroller[1924:146558] propertyattributes:[email protected] "NSString", &,N, Vmyname
Explain:
in the example above, get propertyattributes as: [email protected] "NSString", &,n,vmyname
This is a char * type.
T : Start Letter
@ "NSString" :p the type of roperty. @ indicates that the property is an OC class, and "NSString" indicates the specific OC class name. For example:
ID myname;//@
Uicolor *myname;//@ "Uicolor"
Uitextfield *myname;//@ "Uitextfield"
Customclass *myname;//@ "Customclass", for custom type
int myname;//i, even if is the base data type, only the value of @encode (int) i
&: indicates that the property is retain (strong), except that C indicates that Copy,assign is not represented.
N: indicates nonatomic, if the atomic is not written.
vmyname: v starts with a property name
Additionally, read-write properties: ReadOnly is not written for R,readwrite.
This article is from the "Layne Learning Corner" blog, please be sure to keep this source http://laynestone.blog.51cto.com/9459455/1683972
Obtaining a list of properties for a class through the OC Runtime (runtime)