5

I found out Objective-C object properties can be marked as @dynamic to let compiler know that implementation will be available at runtime. I'd like to know if there is a way to tell the compiler that all properties on an object are dynamic without explicitly specifying them one-by-one (I don't have a list of properties up front). I know that this would not be a problem if I would just use [object something] but for stylistic purposes I want to use object.something syntax.

I'm fairly sure that it's not possible to do that but I'd like someone to confirm that. Since this is not for production use solution can involve anything you can imagine.

Thanks.

Additional info:

  • I only care about -something (getter) working so if your solution does not support setters that is fine.

Example:

@interface MagicalClass : NSObject
// property 'something' is not defined!
@end

@implementation MagicalClass
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { ... }
- (void)forwardInvocation:(NSInvocation *)anInvocation { ... }
@end

MagicalClass *obj = [[MagicalClass alloc] init];
[obj something]; // compiler warning
obj.something; // compiler error
cppforlife
  • 83
  • 5
  • 3
    If you "don't have a list of properties up front," how are you declaring them? Like, `@synthesize` and `@dynamic` tell the compiler how the property will be *implemented*, but how are you declaring that these properties exist at all? – Chuck Sep 29 '11 at 04:52
  • Right this is my problem. I dont want to declare properties. I want compiler to trust me in this particular case because I want to implement them dynamically (just for this class). – cppforlife Sep 29 '11 at 05:00

3 Answers3

4

This really doesn't work with declared properties. The whole point of them is that you declare upfront what your properties are and how you interact with them. If you don't have any to declare, then you don't have any declared properties.

Unfortunately, it also doesn't work well with plain messages, although it can work better than dot syntax. Objective-C's static type checking will throw a hissy-fit of warnings, and if any of the properties are of non-object types, it might not be able to generate the correct calling code.

This kind of thing is common in languages like Python and Ruby where things don't have to be declared, but it just doesn't mesh well with Objective-C. In Objective-C, accessing arbitrary attributes is generally done with strings (cf. Key-Value Coding and NSAttributedString).

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • im just somewhat surprised that it is not possible to let compiler know that i know what im doing. (runtime support for that is already there) – cppforlife Oct 01 '11 at 09:15
2

I don't believe this is possible. If you use the id type, you may be able to send undeclared messages, but dot syntax really relies on knowing about your specific accessors.

andyvn22
  • 14,696
  • 1
  • 52
  • 74
  • that's as far as I got. I still cannot figure out why it was done this way since dot syntax can be translated into message passing style. – cppforlife Sep 29 '11 at 05:20
0

I haven't tried this, but if you provide a getter and setter, does Xcode still want the @synthesize or @dynamic directive?

So if you property is called something, implement -setSomething: and -something.

Thomas Müller
  • 15,565
  • 6
  • 41
  • 47
  • cant do that since I want to implement `-setSomething:` and `-something` at runtime using forwardInvocation and its friends. – cppforlife Sep 29 '11 at 04:35