To support iOS 4.3, use “assign” instead of weak, but Interface Builder uses unsafe_unretained?
-
30-06-2021 - |
سؤال
To support iOS 4.3 with ARC, I think the proper way is to use assign
:
@property (assign, nonatomic) UIView *view;
@property (assign, nonatomic) MYNode *node;
Is that correct? I also see the following in Apple's doc for Transitioning to ARC:
For declared properties, you should use assign instead of weak; for variables you should use __unsafe_unretained instead of __weak.
However, if I use the current Xcode (4.4.1), changing a Single View app target to 4.3, and Ctrl-drag a UIButton to the .h
file to create an outlet, the generated code is:
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *foo;
Why the difference and which one should be used?
المحلول
According to 4.1.1. Property declarations in the llvm documentation "assign" and "unsafe_unretained" are equivalent in a property declaration:
- assign implies __unsafe_unretained ownership.
- ...
- unsafe_unretained implies __unsafe_unretained ownership.
ADDED: In the clang source code http://clang.llvm.org/doxygen/SemaObjCProperty_8cpp_source.html you find
00523 // 'unsafe_unretained' is alias for 'assign'.
00524 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
00525 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
نصائح أخرى
You've quoted excerpt from answer to the question "Which classes don’t support weak references?" — actually, I guess, the excerpt is meant to be applied only to the classes listed in the answer.
From what I've read before when I was studying ARC, there is no real difference between unsafe_unretained
and assign
.