Why the UITableView property attribute is not weak

I found this in the UITableView header file, and almost every property is non-arcane, although my project uses ARC.

@property (nonatomic, assign) id <UITableViewDataSource> dataSource; @property (nonatomic, assign) id <UITableViewDelegate> delegate; 

Why doesn't Apple use the weak property instead of assign , is it backward compatible for non-arcs? If so, why not use __has_feature(objc_arc) to distinguish between ARC and non-ARC.

 #if __has_feature(objc_arc) @property (nonatomic, weak) id <UITableViewDataSource> dataSource; @property (nonatomic, weak) id <UITableViewDelegate> delegate; #else @property (nonatomic, assign) id <UITableViewDataSource> dataSource; @property (nonatomic, assign) id <UITableViewDelegate> delegate; #endif 

I hope the delegate is weak, so I don't need to set the delegate to zero when the delegate instance is freed.

Thank you for your help.

Edit:

I note that __has_feature(objc_arc) incorrect because I can use ARC when my deployment target is 4.3, but then I cannot use weak . So the condition should be: my deployment target is 5.0 or higher.

 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0 @property (nonatomic, weak) id <UITableViewDataSource> dataSource; @property (nonatomic, weak) id <UITableViewDelegate> delegate; #else @property (nonatomic, assign) id <UITableViewDataSource> dataSource; @property (nonatomic, assign) id <UITableViewDelegate> delegate; #endif 
+6
source share
2 answers

Why doesn't Apple use weak property instead of assignment, is it backward compatible for non-arcs?

I did some research and found that __weak is synonymous with the assign modifier. But only the difference in the designation of the delegate instance will not be set to zero when it is released. You typically use an assignment modifier for IBOutlets and delegates. In ARC, this is replaced by __weak. However, there is a reservation. __weak requires you to deploy a runtime application that supports null weak links. This includes:> = (iOS 5 and Lion). Snow Leopard or iOS 4 and older operating systems do not support null weak links. This obviously means that you cannot use __weak modification modifiers if you plan to deploy older systems. So yes, in this case it is backward compatible for non-arcs.

+4
source

Note that when you have a delegate assigned, it is very important to always set the delegation value to zero whenever the object is freed - so the object should always be careful to exclude delegate references in dealloc if it has not done so elsewhere.

Thus, delegate assignment, any given left-handed task, will be invalid after the object is freed (indicating that the memory is no longer allocated for the expected object) - and thus, you may cause a crash if you try to use it. The sign of this in the debugger is when the debugger claims that some variable is of a type that seems completely wrong from what is actually declared to be a variable.

This may be the very reason “Why is not a UITableView delegate property attribute”

Hope this helps you.

0
source

Source: https://habr.com/ru/post/979028/


All Articles