NSProxy 如何“将自身转变为另一个对象”?
通常,发送给代理的消息会转发到真实对象,或者导致代理加载(或将其自身转换为)真实对象。
“变形为实物”到底是如何进行的呢?
为了让事情更具体一些,假设类 Foo
有一个方法 newFooWithString:
,它接受一个字符串并返回 Foo
的新实例。是否可以设置一个位于周围的 NSProxy,如果收到 pleaseBecomeAFooUsingString: @"bar"
消息,则将其自身转换为 [Foo newFooWithString: @ “bar”]
,占用相同的内存,而不与可能存在的其他对其自身的引用混淆?
The NSProxy Class Reference says this:
Typically, a message to a proxy is forwarded to the real object or causes the proxy to load (or transform itself into) the real object.
How exactly would the "transform itself into the real object" work?
To make things a little more specific, suppose class Foo
has a method newFooWithString:
that takes a string and returns a new instance of Foo
. Would it be possible to setup an NSProxy
that sits around, and if a pleaseBecomeAFooUsingString: @"bar"
message is received, transforms itself into [Foo newFooWithString: @"bar"]
, occupying the same memory, without messing with other references to itself that may exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在整个代码中都有一个指向同一个 NSProxy 实例的指针,并且将“转换”它,那么它将在整个代码中发生变化。无法区分对象方法的调用者,因此您将无法自动替换代码中转发方法调用的目标。常见的可转换代理将如下所示:
MyTrickyProxy.h
MyTrickyProxy.m
所以你要求的是某种代码魔法,但 NSProxy 是一个简单的消息转发器,根本没有任何魔法,所以你的目标无法以你所描述的方式实现。
If you have a pointer to the same NSProxy instance all over the code and will "transform" it, it will change all over the code. There is no way to differentiate a caller of a method for an object, so you will not be able to alternate targets for forwarding of methods invocation in your code automatically. Common transformable proxy will looks in following way:
MyTrickyProxy.h
MyTrickyProxy.m
So what you requested is some sort of code-magic, but NSProxy is a simple forwarder of a messages, there is no any magic at all, so your goal is not achievable in a way as you described.
您可以创建 NSProxy 的子类,该子类根据您想要的每个标准来更改将其方法转发到的对象。因此,您的对象将始终指向 NSProxy,但您 pleaseBecomeAFooUsingString: 会将其转发到的对象更改为 Foo。
You could create a subclass of of NSProxy that changes which object it forwards it methods to based of what every criteria you want. So you object will always point to NSProxy but you pleaseBecomeAFooUsingString: will change the the object it forwards to as a Foo.