Very helpful Objective-C method if you're debugging your iPhone, iPad or iPod app.
It writes out all the properties of a custom NSObject!
/** Get a printable string of all properties of an object.
Based on: http://zearfoss.wordpress.com/2011/04/14/objective-c-quickie-printing-all-declared-properties-of-an-object/
@param obj The object to analyze.
@return Returns a ready-for-print string containing all property values.
*/
+ (NSString *)printPropertiesForObject:(id)obj {
NSMutableString *str = [[NSMutableString alloc] init];
NSUInteger count;
objc_property_t *properties = class_copyPropertyList([obj class], &count);
for (NSUInteger i=0; i<count; i++) {
NSString *selector = [NSString stringWithCString:property_getName(properties[i]) encoding:NSUTF8StringEncoding];
SEL sel = sel_registerName([selector UTF8String]);
const char *attr = property_getAttributes(properties[i]);
switch (attr[1]) {
case '@':
[str appendString:[NSString stringWithFormat:@"%s : %@\n", property_getName(properties[i]), objc_msgSend(obj, sel)]];
break;
case 'i':
[str appendString:[NSString stringWithFormat:@"%s : %i\n", property_getName(properties[i]), [objc_msgSend(obj, sel) intValue]]];
break;
case 'f':
[str appendString:[NSString stringWithFormat:@"%s : %f\n", property_getName(properties[i]), [objc_msgSend(obj, sel) floatValue]]];
break;
default:
break;
}
}
free(properties);
return str;
}
Download the ready-for-use source file (.m) of Print all properties value of any custom Object
No comments:
Post a Comment