Is it possible to programmatically get the properties of an object in objective-c without using the runtime methods? I only ask because it seems unnecessary to check it at runtime, when it won’t be changing.
I’m thinking of something to this effect:
MyObject *foo = [[MyObject alloc] init];
NSDictionary *propertiesNamesAndValues = [foo getAllProperties];
Currently my solution looks like this:
id currentClass = [MyObject class];
NSString *propertyName;
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
for (i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
propertyName = [NSString stringWithCString:property_getName(property)];
NSLog(propertyName);
NSLog(@"%@",[foo valueForKey:propertyName]);
}
Simple answer is no.
Introspection by definition happens at runtime.
Additionally, objective-c is highly dynamic language deferring virtually everything to runtime.
In Objective-C, you just don’t have that much syntactic sugar for introspection – you just have plain-C runtime functions.
However, nothing stops you from writing some categories on
NSObjectto provide more Objective-C’ish API and I guess there must be some open source implementations already over internet.