简体   繁体   English

在Objective-C中使用@property和弱属性

[英]Usage of @property and weak properties in Objective-C

I have a specific question and I could not find an answer for it. 我有一个具体的问题,我找不到答案。

I have a storyboard which has some views. 我有一个有一些观点的故事板。 Some of the views have outlets. 一些观点有出口。 I understand that I have to declare my outlets as weak parameters however I don't know if I have to declare getters and setters (with @property and synthesize). 我知道我必须将我的出口声明为弱参数,但我不知道是否必须声明getter和setter(使用@property和synthesize)。

1 - __weak IBOutlet UITableView *table;
2 - @property(nonatomic, weak) UITableView *table; 

If I just declare (1) I can just do "table" on the view controller. 如果我只是声明(1)我可以在视图控制器上执行“table”。

If I declare (1) and (2) I can do self.table. 如果我宣布(1)和(2)我可以自我表达。

What's the difference? 有什么不同? What is the best approach? 什么是最好的方法?

(1) is an instance variable declaration. (1)是实例变量声明。 (2) is a property definition. (2)是属性定义。 If you synthesize the property, or use auto-synthesis, an instance variable is also created in that case. 如果合成属性或使用自动合成,则在这种情况下也会创建实例变量。 Usually, unless you want to expose the view in public API or for polymorphism, it is enough to declare an instance variable. 通常,除非您希望在公共API或多态中公开视图,否则声明实例变量就足够了。

There are some other specific cases where a property may be preferred. 还有一些其他特定情况可能是首选财产。 For instance, if you want to reference a view inside a block but do not wish to retain self , a property is easier to access using the weakSelf paradigm. 例如,如果要引用块内的视图但不希望保留self ,则使用weakSelf范例可以更轻松地访问属性。 But you can create weak references to views also, so this is moot. 但是你也可以创建对视图的弱引用,所以这没有实际意义。

Accessing instance variables is not done using the dot ( . ) notation, but using directly or, less used, the arrow ( -> ) notation. 访问实例变量不是使用点( . )表示法,而是使用直接或较少使用的箭头( -> )表示法。

So either: 所以要么:

[_tableView reloadData];

or 要么

[self->_tableView reloadData];

Remember that using -> on a nil reference results in a bad access. 请记住,在零参考上使用->导致访问不良。

你可以只为两者“表”,你只需要在你的实现中使用@synthesize来合成属性。

The first is an instance variable, the second one is defining a property. 第一个是实例变量,第二个是定义属性。 The convention is to always use properties, which now defaults to auto synthesize, with the iVar named on the convention _varName . 惯例是始终使用属性,现在默认为自动合成,使用在约定_varName上命名的_varName You can then access the variable with _varName or with self.varName . 然后,您可以使用_varNameself.varName访问变量。 It is recommended to always access variables through properties, the only exception being when you are overriding the property's getter. 建议始终通过属性访问变量,唯一的例外是当您覆盖属性的getter时。

There generally is no reason to declare an outlet as a strong property, implying ownership. 通常没有理由将出口声明为强势财产,这意味着所有权。 Most views are owned by their superviews. 大多数视图都归其超级视图所有。

@property (weak) IBOutlet UITableView *table;

You then treat is as any other property 然后你就像任何其他财产一样对待

@synthesize table = _table;
- (void)someMethod
{
     [self.table doSomething ....]
}

See also Managing the Lifetimes of Objects from Nib Files 另请参阅从Nib文件管理对象的生命周期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM