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).
精彩评论