gpt4 book ai didi

ios - 对象自动设置为零

转载 作者:行者123 更新时间:2023-11-29 02:58:09 25 4
gpt4 key购买 nike

首先是我的代码:

- (void)viewDidLoad {
[super viewDidLoad];

self.elements = [NSMutableArray arrayWithObjects:@"Object 1", @"Object 2", @"Object 3", nil];
self.elements2 = @"Lorem ipsum";
NSLog(@"%@", self.elements);

[self loadContent];
}

他打印出,_elements 在 loadContent 之后也被正确初始化,_elements 包含三个对象。但是在 viewDidLoad 方法之后直接调用的 numberOfComponentsInPickerView: 方法后面只有几行,_elements 是 nil。我不知道为什么我的应用程序将 _elements 设置为 nil

谢谢 iComputerfreak


编辑

我将我的完整代码上传到 gist.github: https://gist.github.com/iComputerfreak/fa3d3ee925c45cd6e1f2


控制台输出

好的,这里是我的控制台输出:

// in viewDidLoad (after setting): 
2014-05-13 16:56:14.292 HFG Vertretungsplan[1051:60b] (
"Object 1",
"Object 2",
"Object 3"
)
// numberOfComponents called automatically:
2014-05-13 16:56:14.298 HFG Vertretungsplan[1051:60b] numberOfComponentsInPickerView: (null)
2014-05-13 16:56:14.303 HFG Vertretungsplan[1051:60b] numberOfComponentsInPickerView: (null)
// viewWillAppear method reached:
2014-05-13 16:56:14.306 HFG Vertretungsplan[1051:60b] View will appear
2014-05-13 16:56:14.306 HFG Vertretungsplan[1051:60b] Elements in viewWillAppear: ( // NSLog("%@", self.elements);
"Object 1",
"Object 2",
"Object 3"
)
// [self.elementsPicker reloadAllComponents];
2014-05-13 16:56:14.307 HFG Vertretungsplan[1051:60b] numberOfComponentsInPickerView: (null)
2014-05-13 16:56:14.308 HFG Vertretungsplan[1051:60b] Elements after reloadAllComponents: (
"Object 1",
"Object 2",
"Object 3"
)

所以只有在重新加载组件时元素才为零。前后都有内容。

最佳答案

覆盖你的_elements getter

- (NSMutableArray *)elements
{
if (!_elements)
_elements = [[NSMutableArray alloc] init];
return _elements;
}

第一次调用_elements就像

self.elements

关于ios - 对象自动设置为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23631937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com