Reflection/List properties: Difference between revisions

Content deleted Content added
added objc
Line 73: Line 73:
//[["name", "obj"], ["obj", true], ["doStuff", function()]]
//[["name", "obj"], ["obj", true], ["doStuff", function()]]
</lang>
</lang>

=={{header|Objective-C}}==
<lang objc>#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface Foo : NSObject {
int exampleIvar;
}
@property (nonatomic) double exampleProperty;
@end
@implementation Foo
- (instancetype)init {
self = [super init];
if (self) {
exampleIvar = 42;
_exampleProperty = 3.14;
}
return self;
}
@end

int main() {
id obj = [[Foo alloc] init];
Class clazz = [obj class];

NSLog(@"\Instance variables:");
unsigned int ivarCount;
Ivar *ivars = class_copyIvarList(clazz, &ivarCount);
for (unsigned int i = 0; i < ivarCount; i++) {
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar);
const char *typeEncoding = ivar_getTypeEncoding(ivar);
// for simple types we can use Key-Value Coding to access it
// but in general we will have to use object_getIvar and cast it to the right type of function
// corresponding to the type of the instance variable
id value = [obj valueForKey:@(name)];
NSLog(@"%s\t%s\t%@", name, typeEncoding, value);
}
free(ivars);

NSLog(@"");
NSLog(@"Properties:");
unsigned int propCount;
objc_property_t *properties = class_copyPropertyList([Foo class], &propCount);
for (unsigned int i = 0; i < propCount; i++) {
objc_property_t p = properties[i];
const char *name = property_getName(p);
const char *attributes = property_getAttributes(p);
// for simple types we can use Key-Value Coding to access it
// but in general we will have to use object_getIvar and cast it to the right type of function
// corresponding to the type of the instance variable
id value = [obj valueForKey:@(name)];
NSLog(@"%s\t%s\t%@", name, attributes, value);
}
free(properties);

return 0;
}</lang>
{{out}}
<pre>
Instance variables:
exampleIvar i 42
_exampleProperty d 3.14

Properties:
exampleProperty Td,N,V_exampleProperty 3.14
</pre>


=={{header|Python}}==
=={{header|Python}}==