NSProxy
seems to work very well as stand-in objects for those that don\'t yet exist. For example.
- (NSMethodSignature *)methodSignatureForSelector:
I don't have the exact same use case (no bindings) of OP but mine was similar: I am creating an NSProxy subclass that presents itself as another object that is actually loaded from a server. During the load, other objects can subscribe to the proxy and the proxy will forward the KVO as soon as the object arrives.
There is a simple NSArray
property in the proxy that records all observers. Until the real object is loaded, the proxy returns nil
in valueForKey:
. When the realObject
arrives, the proxy calls addObserver:forKeyPath:options:context:
on the real object and then, through the magic of the runtime, walks through all properties of realObject
and does this:
id old = object_getIvar(realObject, backingVar);
object_setIvar(realObject, backingVar, nil);
[realObject willChangeValueForKey:propertyName];
object_setIvar(realObject, backingVar, old);
[realObject didChangeValueForKey:propertyName];
This seems to work, at least I haven't gotten any KVO compliance errors yet. It does make sense though, first all properties are nil and then they change from nil to the actual value. It is all like ipmcc said in his first statement above, so this post is just a confirmation! Note that the second surrogate that he proposed actually isn't needed, you just have to keep track of observers yourself.