1

Sometimes I need to implement proxy pattern in ObjC. I need it in cases were I create inner subject in runtime and do not want to move creation logic out from the proxy. Sometimes I use couple of objects inside the proxy, also I prefer use ARC to memory menegment. Now I implement it using C++ style:

- (void)setProperty:(CGFloat)value
{
    _innerObject.value = value;
}


- (CGFloat)property
{
    return _innerObject.value;
}

<...> 

I think that it is not a best way, I think that exist more easy way. I want to use ObjC runtime and forward messages automatically.
How I can do it without write every set/get method by hand?

Andrew Romanov
  • 4,774
  • 3
  • 25
  • 40

1 Answers1

0

- forwardingTargetForSelector: "[r]eturns the object to which unrecognized messages should first be directed". So:

// Will be queried for every message that is sent to `self` but
// which `self` does not itself implement.
- (id)forwardingTargetForSelector:(SEL)selector
{
    return _innerObject;
}
Tommy
  • 99,986
  • 12
  • 185
  • 204
  • As I understand, I have to add this method to my class and remove my implementations. Am I right? How I should implement properties? May be I should use dynamic? – Andrew Romanov May 18 '16 at 03:51
  • Sorry for the slow reply: yes. There's also a `-forwardInvocation:` which would allow you to capture the full content of messages supplied to your proxy in case you want to dispatch them later rather than now, but it's a slower mechanism than `-forwardingTargetForSelector:`. – Tommy May 18 '16 at 16:14