Is it possible to forward a property?
Let's say I have a proxy class that has a member which has in turns a property called string
.
I want my proxy class to have the same property also named string
. From there, I would like that whenever I call the getter/setter on the proxy, it set/get value on its class member.
the closest I have come to it is to implement -forwardin开发者_如何转开发gTargetForSelector
and switch the target when needed but it's not clean nor pratical (it gives warning and you cannot use the dot-syntax)
You can declare the properties as @dynamic
and implement these two methods to handle the forwarding - methodSignatureForSelector:
and forwardInvocation:
. Implementation will be something has described below –
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
if ( alpha && [alpha respondsToSelector:aSelector] ) {
return [alpha methodSignatureForSelector:aSelector];
} else {
return [super methodSignatureForSelector:aSelector];
}
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
if ( alpha && [alpha respondsToSelector:[anInvocation selector]] ) {
[anInvocation invokeWithTarget:alpha];
} else {
[super forwardInvocation:anInvocation];
}
}
精彩评论