Dynamic objecing of objective-C object method to modify NULL pointer

Source: Internet
Author: User

Background: Currently, many IOS projects convert JSON data into an object for storage. Assume that this object has an attributes named myname of the nsstring type, and this attributes is always nil. After this object is created, it is not assigned a value. Now we need to use a function to detect it and assign it to @ "";


In addition, I do not know how many attributes are available. If the nsbutes is of the nsstring type and the attributes is nil, the value is assigned to @ "".


The Code is as follows:

//

// Gmcommonhelper. h

# Import <Foundation/Foundation. h>

# Import <objc/runtime. h>



@ Interface gmcommonhelper: nsobject


+ (Void) fixnildata :( ID) kclass;


@ End


//

// Gmcommonhelper. m

# Import <adsupport/adsupport. h>


# Import "gmcommonhelper. H"

@ Implementation gmcommonhelper


+ (Void) fixnildata :( ID) kclass

{

Unsigned int propertycount = 0;

Objc_property_t * properties = class_copypropertylist ([kclass class], & propertycount );

For (unsigned int I = 0; I <propertycount; ++ I ){

Objc_property_t property = properties [I];

Const char * name = property_getname (property );

Const char * attrs = property_getattributes (property );

Nsstring * utf8name = [nsstring stringwithuf8string: Name];

Nsstring * utf8attrs = [nsstring stringwithuf8string: attrs];

Id propertval = [kclass valueforkey: utf8name];

If (! Propertval & [utf8attrs hasprefix: @ "[email protected] \" nsstring \ ""]) {

[Kclass setvalue: @ "" forkey: utf8name];

}

}

Free (properties );

}



//-----------------------


-(Void) releaseproperties {unsigned int C = 0; objc_property_t * properties = class_copypropertylist ([self class], & C); For (unsigned int I = 0; I <C; I ++) {objc_property_t property = properties [I]; nsstring * propertyname = [nsstring stringwithuf8string: property_getname (property)]; nsstring * propertytype = [nsstring stringwithuf8string: property_getattributes (property)]; If ([propertytype hasprefix: @ "[email protected]"] // is an object & [propertytype rangeofstring: @ ", R,"]. location = nsnotfound // not readonly) {[self setvalue: Nil forkey: propertyname]; nslog (@ "% @. % = % @ ", nsstringfromclass (CLS), propertyname, [self valueforkey: propertyname]) ;}} free (properties );}

//-----------------------



If you only care about classes you can useisKindOfClass:, But if you want to deal with scalars you are correct that you need to useproperty_getAttributes(), It returns a string that encodes the type information. Below is a basic function that demonstrates what you need to do. For examples of encoding strings, look here.

unsigned int outCount, i;objc_property_t *properties = class_copyPropertyList([object class], &outCount);for (i = 0; i < outCount; i++) {    objc_property_t property = properties[i];    char *property_name = property_getName(property);    char *property_type = property_getAttributes(property);    switch(property_type[1]) {      case ‘f‘ : //float        break;      case ‘s‘ : //short        break;      case ‘@‘ : //ObjC object        //Handle different clases in here        break;    }}

Obvviusly you will need to add all the types and classes you need to handle to this, it uses the normal objc @ encode type.

Related Article

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.