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

Source: Internet
Author: User

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

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

# Import



@ Interface GMCommonHelper: NSObject


+ (Void) fixNilData :( id) kClass;


@ End


//

// GMCommonHelper. m

# Import


# 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: @ "T @ \" 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: @ "T @"] // 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.