简体   繁体   English

如何使用具有自动引用计数的委托

[英]how to use delegates with Automatic Reference Counting

I've jumped on the ARC bandwagon.我已经加入了 ARC 的潮流。 In the past I would have my delegate properties declared like this:在过去,我会像这样声明我的委托属性:

@property(assign) id<MyProtocol> delegate;

So I thought I would do this under ARC:所以我想我会在 ARC 下这样做:

@property(weak) id<MyProtocol> delegate;

Not so.不是这样。 On the @synthesize statement in the.m I have a compile error :在.m 中的@synthesize 语句中出现编译错误

*Semantic Issue: Existing ivar 'delegate' for __weak property 'delegate' must be __weak* *语义问题:__weak 属性 'delegate' 的现有 ivar 'delegate' 必须是 __weak*

I HAVE declared it as weak though.我已经宣布它很弱。 Also how do I pass a class implementing a protocol to a weakly referenced property?另外,如何将实现协议的 class 传递给弱引用属性? Do I have to wrap it in one of those weird obj_unretained calls?我是否必须将其包装在那些奇怪的 obj_unretained 调用之一中?

Any help on this would be very much appreciated.对此的任何帮助将不胜感激。

"ivar" means "instance variable", which you have not shown. “ivar”表示“实例变量”,您没有显示。 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;

If the property claims to be weak, the ivar that the value ends up being stored in must be weak as well.如果该属性声称是弱的,那么最终存储该值的 ivar 也必须是弱的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM