iOS: __weak vs (weak)

大兔子大兔子 提交于 2020-02-01 05:04:27

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/23944361/ios-weak-vs-weak

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