可以转发属性吗?

发布于 2024-11-09 14:52:05 字数 275 浏览 0 评论 0原文

可以转让房产吗?

假设我有一个代理类,它有一个成员,而该成员又具有一个名为 string 的属性。

我希望我的代理类具有相同的属性,也名为 string。从那里开始,我希望每当我在代理上调用 getter/setter 时,它都会在其类成员上设置/获取值。

我最接近的是实现 -forwardingTargetForSelector 并在需要时切换目标,但它不干净也不实用(它会发出警告,并且您不能使用点语法)

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 -forwardingTargetForSelector and switch the target when needed but it's not clean nor pratical (it gives warning and you cannot use the dot-syntax)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜血染红嫁衣 2024-11-16 14:52:05

您可以将属性声明为 @dynamic 并实现这两个方法来处理转发 - methodSignatureForSelector:forwardInspiration:。实施将如下所述 -

- (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];
    }
}

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文