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?

回答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.