0

Is it possible to give an instance of object to a function and the function return the value of all attributes for this instance ?? the function should work for any class

Emil
  • 7,220
  • 17
  • 76
  • 135
  • 2
    Note that such a code is usually just a workaround for bad architecture. Although it is often useful for debugging or testing. – Sulthan May 26 '13 at 15:19
  • What @Sulthan said. If this code and the answers to your other questions are used beyond debugging or pure academic curiosity, you are heading down a path that will produce a very difficult, very fragile, codebase. Objective-C was simply not designed for this level of introspection based programming. – bbum May 26 '13 at 18:07

1 Answers1

2

This is the solution:

unsigned int numberOfAttributs;
objc_property_t *attributes = class_copyPropertyList([obj class], &numberOfAttributes);

for (unsigned int i = 0; i < numberOfAttributes; i++) {

    objc_property_t attribute = attributes[i];  
    NSString *attributeName = [NSString stringWithUTF8String: property_getName(attribute)];
    NSLog(@"%@", attributeName);
    id attributeValue = [obj valueForKey:attributeName];
    NSLog(@"%@", attributeValue);
}
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51