Reputation: 38035
I have a question similar to this one, but I'm curious to know if there's any way (compiler flag, @-directive, etc.) that lets a class have all possible properties considered @dynamic at compile time, such that you could code against any arbitrary property without having to declare it as @dynamic explicitly.
For example I could just do this:
myObject.anyPropertyName = someObject;
...much like you can do in JavaScript, without having to declare @dynamic anyPropertyName
.
Upvotes: 0
Views: 259
Reputation:
What you want is not possible. The compiler will not let you send any message without that selector being declared before. This of course includes accessing properties. This is necessary so the compiler can know what types to expect - not everything is an object. The compiler must generate different code for objects (think of ARC), structs or scalars.
The runtime on the other hand would be happy to handle this without having declared those properties. But then your job would be to call the right variant of objc_msgSend
casted to the right signature. This of course requires code that is much uglier than just using valueForKey:
and setValue:forKey:
directly.
Also @dynamic
doesn't really have anything to do with that. All @dynamic
does is shut up the compiler about missing implementations for getters and setters and preventing it from automatically generating them. It's a way for promising the compiler that at runtime those messages are going to be handled. Using @dynamic doesn't automatically forward property access to valueForKey: and setValue:forKey:.
Upvotes: 4