iOS: __weak vs (weak)

2019-04-09 17:05发布

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?

2条回答
祖国的老花朵
2楼-- · 2019-04-09 17:45

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 and (weak) is the same thing, but the second is used as attribute for properties.

查看更多
时光不老,我们不散
3楼-- · 2019-04-09 17:52

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.

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).

查看更多
登录 后发表回答