工程启用ARC后,delegate不能设置成assign属性了,只能是unsafe_unretained么
//.h id __unsafe_unretained _delegate; @property(nonatomic,unsafe_unretained) id <EGORefreshTableHeaderDelegate> delegate; //.m @synthesize delegate=_delegate; if (!_delegate) { return; }
unsafe_unretained的指针应该如何避免对象指向丢失,一旦丢失,即使是上面的if判断调用都会crash,如何处理呢
求教!!!
正确的写法是
__unsafe_unretained id<EGORefreshTableHeaderDelegate> _delegate;
@property(nonatomic,assign) id <EGORefreshTableHeaderDelegate> delegate;
crash和此处无关,是你别的地方已经释放了此对象。
如果此处改为强引用,则不可避免的造成retain cycle从而内存泄露,是万万不能的。你要做的是检查为何释放后还会被回调。
开启ARC后,如果部署iOS版本是5.0以上,请使用weak,5.0以下的才用unsafe_unretained,楼上说的设置成strong是不对的
请看官方的说明:
~~考虑到delegate的类型不确定性与非拥有性,毫无疑问:~~
由于delegate的非拥有性,在ARC下应该首选
weak
,因为它可以防止野指针问题,更加安全。但如果在iOS4下,由于还未支持
weak
,就只能退而求其次,使用unsafe_unretained
了。开启ARC后,不需要设置成
unsafe_unretained
,设置成strong
即可