Obtaining a list of properties for a class through the OC Runtime (runtime)

Source: Internet
Author: User

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)

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.