小编Ben*_*int的帖子

为什么要分配一个强大的财产工作,而不是弱工作?

我在我的.h文件中声明了一个属性

@property (weak, nonatomic) UIPickerView *levelPicker;
Run Code Online (Sandbox Code Playgroud)

在我的实现文件中合成为:

@synthesize levelPicker = _levelPicker;
Run Code Online (Sandbox Code Playgroud)

然后我在同一个实现文件中有一个代码块,它执行以下操作:

if (self.levelPicker == nil) {
    self.levelPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
    self.levelPicker.delegate = self;
    self.levelPicker.dataSource = self;
}
textField.inputView = self.levelPicker;
Run Code Online (Sandbox Code Playgroud)

在这种情况下,self._levelPicker未设置为新的UIPickerView.即self.levelPicker = blah赋值不起作用.

但是,如果我将属性声明更改为:

@property (strong, nonatomic) UIPickerView *levelPicker;
Run Code Online (Sandbox Code Playgroud)

然后一切都按预期工作,_levelPicker设置为新分配的UIPickerView.

有人可以告诉我为什么会这样吗?我以为我正在理解参考是如何工作的,但我想我还有更多要学习的东西.我读了一些其他相关的SO帖子,但对我来说仍然不完全清楚.

weak-references objective-c ios automatic-ref-counting

3
推荐指数
1
解决办法
4020
查看次数