0

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.

Community
  • 1
  • 1
devios1
  • 36,899
  • 45
  • 162
  • 260
  • 1
    I don't understand what you're asking that's different from the question you linked. Can you clarify? – jscs Mar 13 '15 at 18:54
  • Specifically I want the ability for me to use any valid identifier as a dot-notation property access, without having to `@dynamic` it on the class first, and have that call into an overridden `setValue:forKey:` method on the class. – devios1 Mar 13 '15 at 21:21
  • 1
    The "everything should be dynamic and at runtime always" phase of iOS development learning. :) It seems enticing, but you'll lose all of the compiler's ability to double-check types and logic. In practice, you're better off leaning on the compiler as much as possible to vet your code while writing any kind of purpose built dynamism as algorithms-- that can be validated in code-- within your app. – bbum Mar 15 '15 at 17:20
  • @bbum Why do you make the presumption that I am just learning iOS development? Besides the fact that we as developers never stop learning, which I somewhat doubt you meant by it, I find your statement somewhat condescending. I am well aware what I lose in moving towards a more dynamic system. There are situations where I would gladly trade compile-time checking for the abilities dynamic programming affords. – devios1 Mar 16 '15 at 18:53
  • 1
    @devios As you say, we area always learning. I said what I did because in my 25 years of Objective-C experience as coder and teacher, I have yet to run into a single curious developer who doesn't go down this path at some point; sometimes when they are starting, sometimes after a couple of decades. It wasn't meant as offense. – bbum Mar 20 '15 at 17:12

1 Answers1

4

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

Sven
  • 22,475
  • 4
  • 52
  • 71
  • I didn't expect it was possible, but I'm always looking for ways to do things more dynamically. ;) I realize it doesn't forward, but it does allow you to use an identifier as a property accessor without the property accessor itself actually being defined at compile time, so I thought it maybe a small step to wonder if there was a way to do that as the default for a class. Anyway thanks. – devios1 Mar 13 '15 at 22:31