开发者

Declaration of properties at runtime?

开发者 https://www.devze.com 2023-02-10 18:20 出处:网络
Is there a way to declare properties at runtime in Obj-C? I was looking for something like class_addMethod but开发者_如何学Python for properties.A common misconception is that @property implies code g

Is there a way to declare properties at runtime in Obj-C? I was looking for something like class_addMethod but开发者_如何学Python for properties.


A common misconception is that @property implies code generation. It does not. @synthesize does that. So, really, your question is likely Is there anyway to generate setter/getter methods at runtime?

The answer is, of course, yes and you would use exactly what you've already found.


Properties are syntactic sugar for getter/setter methods.
You can't declare them at runtime. As a comment above said, how would you use them?
But you can declare them and add implementation at runtime. The @synthesize keyword simply installs a default getter/setter implementation. To prevent this, use the @dynamic keyword, and provide the implementation at runtime.

You might want to look at a lighter weight approach such as intercepting messages (NSObject>>(id)forwardingTargetForSelector and related methods).

0

精彩评论

暂无评论...
验证码 换一张
取 消