k06a
k06a

Reputation: 18745

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.

Upvotes: 1

Views: 346

Answers (2)

k06a
k06a

Reputation: 18745

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+.

Upvotes: 1

Mobile Ben
Mobile Ben

Reputation: 7331

I don't think you can actually do this. Class properties cannot be synthesized nor are they auto synthesized. You're responsible for implementing the getter/setter or using @dynamic. This appears to be syntactic sugar for class level getter/setter methods and hence are not showing up.

I wonder if this is a temporary thing?

What specifically are you trying to do?

Upvotes: 0

Related Questions