如何使用具有自动引用计数的委托 [英] how to use delegates with Automatic Reference Counting

查看:98
本文介绍了如何使用具有自动引用计数的委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跳过了ARC的潮流。在过去,我会将我的委托属性声明为:

I've jumped on the ARC bandwagon. In the past I would have my delegate properties declared like this:

@property(assign) id<MyProtocol> delegate;

所以我想我会在ARC下这样做:

So I thought I would do this under ARC:

@property(weak) id<MyProtocol> delegate;

不是这样。在.m中的@synthesize语句中,我有一个编译错误

Not so. On the @synthesize statement in the .m I have a compile error:

*语义问题:__ weak属性的现有ivar'委托'代表'必须是__weak *

*Semantic Issue: Existing ivar 'delegate' for __weak property 'delegate' must be __weak*

我已经声明它虽然很弱!另外,我如何将实现协议的类传递给弱引用的属性。我是否必须将其包裹在其中一个奇怪的obj_unretained调用中?

I HAVE declared it as weak though! Also how do I pass a class implementing a protocol to a weakly referenced property. Do I have to wrap it in one of those weird obj_unretained calls?

对此的任何帮助都将非常感激。

Any help on this would be very much appreciated.

推荐答案

ivar表示实例变量,您尚未显示。我认为它看起来像这样:

"ivar" means "instance variable", which you have not shown. I'm betting it looks something like this:

@interface Foo : NSObject {
    id delegate;
}

@property (weak) id delegate;

错误的含义是它必须如下所示:

What the error is saying is that it must look like this:

@interface Foo : NSObject {
    __weak id delegate;
}

@property (weak) id delegate;

如果财产声称属弱,则该值最终存储的ivar必须较弱还有。

If the property claims to be weak, the ivar that the value ends up being stored in must be weak as well.

这篇关于如何使用具有自动引用计数的委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆