Search code examples
objective-cllvm-clangobjective-c-runtime

How to access Objective-C class properties in runtime?


Thats what I have:

@interface Example : NSObject

@property (class) NSString *classProperty;

@end

And trying to access class property:

Class meta = objc_getMetaClass(class_getName([Example class]));
objc_property_t property = class_getProperty(meta, "classProperty"); // == nil
objc_property_t *properties = class_copyPropertyList(meta, NULL); // == nil

I am using Xcode 8 and iPhone 7 iOS 10 Simulator and just wanna access class properties with Obj-C runtime functions.

I have read some slides here: https://developer.apple.com/videos/play/wwdc2016/405/ And read some info here: http://useyourloaf.com/blog/objective-c-class-properties/

Also checked #if __has_feature(objc_class_property), and it has: Macro to detect availability of class properties in Objective-C

UPDATE:

Strange things happens: today this code works fine. I am sure yesterday I had a moment when it works, but later it was not.

UPDATE 2:

Read my own answer below.


Solution

  • Found absolutely different behaviour when setting Deployment Target to iOS 8 and iOS 9+. Objective-C runtime have no information about class properties when Deployment Target is set to iOS 8, and have all necessary info when Deployment Target is set to iOS 9+. So framework I am working on will become iOS 9+.