iOS get a list of properties for a class

Source: Internet
Author: User

explained by examples :

@interface Demoobject:nsobject@property (Strong, nonatomic,readonly) NSString *name; @property (Strong, Nonatomic) Nsmutablearray *datasource; @property (copy, nonatomic) nsdictionary *product; @property (assign, Atomic) Nsuinteger Count; @property (weak, nonatomic) Demoobject *object; @end

    unsigned int count;    objc_property_t *properties = Class_copypropertylist ([Demoobject class], &count);    for (int i = 0; i < count; i++)    {        objc_property_t = properties[i];                NSLog (@ "name:%s", Property_getname (property));        NSLog (@ "attributes:%s", property_getattributes);    }    Free (properties);

Print the following information:

2015-04-28 14:59:16.421 apptest[11137:94568] Name:name
2015-04-28 14:59:16.422 apptest[11137:94568] attributes:[email protected] "NSString", R,n,v_name
2015-04-28 14:59:16.422 apptest[11137:94568] Name:datasource
2015-04-28 14:59:16.422 apptest[11137:94568] attributes:[email protected] "Nsmutablearray", &,n,v_datasource
2015-04-28 14:59:16.422 apptest[11137:94568] Name:product
2015-04-28 14:59:16.422 apptest[11137:94568] attributes:[email protected] "nsdictionary", c,n,v_product
2015-04-28 14:59:16.422 apptest[11137:94568] Name:count
2015-04-28 14:59:16.422 apptest[11137:94568] Attributes:tq,v_count
2015-04-28 14:59:16.422 apptest[11137:94568] Name:object
2015-04-28 14:59:16.422 apptest[11137:94568] attributes:[email protected] "Demoobject", W,n,v_object


Description

If we want to get the string, we want to get it through UTF8 encoding, as follows:

NSString *name = [NSString Stringwithutf8string:property_getname (property)]; NSString *attributes = [NSString stringwithutf8string:property_getattributes (property)];

Attributes are fixed format, separated by commas, usually [email protected] "type" at the beginning of the V_ property name end of the format, in the middle of the following table describes:


You can use the Property_getattributes function to discover the name, the @encode type string of a property, and other ATT Ributes of the property.

The string starts with a T followed by the @encode type and a comma, and finishes with a V followed by the name of the BAC King instance variable. Between these, the attributes is specified by the following descriptors, separated by commas:



There are other types of attribute instances that can be searched for Apple Documents "propertyAttribute Description Examples"


Use:

1. When we send the network request, we receive the data model of the server reply, we do not need to parse the JSON dictionary into our model class in each data model, we can implement this function through a base class, all the models inherit the base class and OK, This function can simply get the property name through the property list, the property type (the property type corresponds to the JSON model's key) and then assign the value.

2. Serialization and deserialization of a custom class.


iOS get a list of properties 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.