I want to get the values of all attributes of an object in Objective-C.
For example I have an instance of my class Client
, named Client1
.
I would like to have a method which get this object as a parameter and returns the attributes and values. (name = 'nameC1'
, age = 21
, ...)
Asked
Active
Viewed 335 times
0

HAS
- 19,140
- 6
- 31
- 53

user2421041
- 29
- 5
-
Do you know the attributes of it up front or are you asking how to enumerate the properties of a class? – NG. May 25 '13 at 20:18
-
Hi , the function should work for any object... ( enumerate all attribut + their values ) – user2421041 May 25 '13 at 20:26
-
Any particular object class? Or simply any objects? – uchuugaka May 25 '13 at 20:54
-
Any particular object class ... It's possible to do this ? – user2421041 May 25 '13 at 21:08
-
What does that have to do with `gnustep`? Is it important? – HAS May 25 '13 at 21:28
-
1I encountered a similar question few time ago: http://stackoverflow.com/questions/16304483/debug-obtain-a-list-of-all-instance-variables-of-an-object-unknown-type/16305101 – Ramy Al Zuhouri May 25 '13 at 22:07
-
@RamyAlZuhouri does that handle primitive types such as ints? – powerj1984 May 25 '13 at 22:11
-
@powerj1984 Yes, it does. For example the type of an int will appear as @"i" in the dictionary. In this case an extra job is needed to find the values of every ivar, by iterating all the keys and call *valueForKey:* on the object of which you want to analyze the ivars. – Ramy Al Zuhouri May 25 '13 at 22:38
-
@user2421041 You really don't want to go down this path. It'll only lead to pain. Your data model should be well defined, with a known and controlled set of properties and relationships. Trying to automate persistence and/or visualization through deep runtime introspection just leads to a fragile and unmaintainable codebase. – bbum May 25 '13 at 22:43
1 Answers
2
This question was a bit different, but I think the answer is the same as the answer to your question:
https://stackoverflow.com/a/8380836/104527
// PropertyUtil.h
#import
@interface PropertyUtil : NSObject
+ (NSDictionary *)classPropsFor:(Class)klass;
@end
// PropertyUtil.m
#import "PropertyUtil.h"
#import "objc/runtime.h"
@implementation PropertyUtil
static const char * getPropertyType(objc_property_t property) {
const char *attributes = property_getAttributes(property);
printf("attributes=%s\n", attributes);
char buffer[1 + strlen(attributes)];
strcpy(buffer, attributes);
char *state = buffer, *attribute;
while ((attribute = strsep(&state, ",")) != NULL) {
if (attribute[0] == 'T' && attribute[1] != '@') {
// it's a C primitive type:
/*
if you want a list of what will be returned for these primitives, search online for
"objective-c" "Property Attribute Description Examples"
apple docs list plenty of examples of what you get for int "i", long "l", unsigned "I", struct, etc.
*/
return (const char *)[[NSData dataWithBytes:(attribute + 1) length:strlen(attribute) - 1] bytes];
}
else if (attribute[0] == 'T' && attribute[1] == '@' && strlen(attribute) == 2) {
// it's an ObjC id type:
return "id";
}
else if (attribute[0] == 'T' && attribute[1] == '@') {
// it's another ObjC object type:
return (const char *)[[NSData dataWithBytes:(attribute + 3) length:strlen(attribute) - 4] bytes];
}
}
return "";
}
+ (NSDictionary *)classPropsFor:(Class)klass
{
if (klass == NULL) {
return nil;
}
NSMutableDictionary *results = [[[NSMutableDictionary alloc] init] autorelease];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(klass, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
const char *propName = property_getName(property);
if(propName) {
const char *propType = getPropertyType(property);
NSString *propertyName = [NSString stringWithUTF8String:propName];
NSString *propertyType = [NSString stringWithUTF8String:propType];
[results setObject:propertyType forKey:propertyName];
}
}
free(properties);
// returning a copy here to make sure the dictionary is immutable
return [NSDictionary dictionaryWithDictionary:results];
}
@end
To use import PropertyUtil.h and do something like:
NSDictionary props = [PropertyUtil classPropsFor:[YourClass class]];

Community
- 1
- 1

powerj1984
- 2,216
- 1
- 22
- 34
-
1Answers the concrete question. However, don't use this kind of code. While such automated, deep introspection, seems like a time saver, it will lead to a fragile, unmaintainable, code base. – bbum May 25 '13 at 22:44
-