[英]iOS: __weak vs (weak)
Are there a differences between these two lines of code? 这两行代码之间是否存在差异?
__weak IBOutlet UITextField *usernameField;
@property (weak) IBOutlet UITextField *usernameField;
What if you declare either of these in interface section of the .h or the .m files? 如果在.h或.m文件的接口部分声明其中任何一个怎么办?
Yes. 是。 The first example declares a weak instance variable called usernameField
, but the second declares a weak property called usernameField
, and an instance variable called _usernameField
that is accessed by the property. 第一示例声明称为弱实例变量usernameField
,但第二个声明了一个弱属性调用usernameField
,并称为一个实例变量_usernameField
由所述属性进行访问。
If you declare it in an @interface section of the .m file, then it can only be accessed in that .m file (unless you mess with the Objective-C runtime). 如果在.m文件的@interface部分声明它,那么只能在该.m文件中访问它(除非你搞乱了Objective-C运行时)。
The difference is not in the weak
reference but just in the fact that the first is an instance
and the second is a @property
. 区别不在于weak
引用,而在于第一个是instance
,第二个是@property
。
__weak
and (weak)
is the same thing, but the second is used as attribute for properties. __weak
和(weak)
是一回事,但第二个用作属性的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.