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.