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.