how to use delegates with Automatic Reference Counting

荒凉一梦 提交于 2019-11-30 10:54:15

问题


I've jumped on the ARC bandwagon. 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:

@property(weak) id<MyProtocol> delegate;

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

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

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.


回答1:


"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;

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



来源:https://stackoverflow.com/questions/6529191/how-to-use-delegates-with-automatic-reference-counting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!