Why does Apple use assign rather than weak to store a delegate?

走远了吗. 提交于 2020-01-04 10:18:08

问题


Some Cocoa and Cocoa Touch classes declare their delegate properties as assign rather than weak, which forces users of the class to nil out the property in dealloc

-(void)dealloc
{
    self.imageScrollView.delegate = nil;
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

Which is very cumbersome.

Why would Apple do it this way?


回答1:


The reason why is that not all system classes have been compiled with ARC.

You may implement a dealloc method if you need to manage resources other than releasing instance variables. You do not have to (indeed you cannot) release instance variables, but you may need to invoke [systemClassInstance setDelegate:nil] on system classes and other code that isn’t compiled using ARC.

See this page on developer.apple.com: Transitioning to ARC Release Notes



来源:https://stackoverflow.com/questions/20419317/why-does-apple-use-assign-rather-than-weak-to-store-a-delegate

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