数组消失

发布于 2024-09-10 09:11:26 字数 1044 浏览 3 评论 0原文

我所在的视图有 UIPickerView。这是我制作排序数组的地方。

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];

   NSMutableArray *studentNames = [[NSMutableArray alloc] init];

   for (Student *student in course.students)
      [studentNames addObject:student.name];

   sortedArray = [studentNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

   [picker selectRow:(kRowMultiplier*sortedArray.count)/2 inComponent:0 animated:NO];
}

我可以在这两种方法中执行 NSLog([sortedArray ComponentsJoinedByString:@", "]); 并且它有效

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

但是

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

当我在此方法中执行相同的跟踪时它不起作用(它崩溃了):

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

我不明白为什么sortedArray除了在这一种方法之外在任何地方都可以工作。

I'm in a view that has a UIPickerView. Here is where I'm making my sortedArray.

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];

   NSMutableArray *studentNames = [[NSMutableArray alloc] init];

   for (Student *student in course.students)
      [studentNames addObject:student.name];

   sortedArray = [studentNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

   [picker selectRow:(kRowMultiplier*sortedArray.count)/2 inComponent:0 animated:NO];
}

I can do an NSLog([sortedArray componentsJoinedByString:@", "]); in these two methods and it works:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

and

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

but when I do that same trace in this method it doesn't work (It crashes):

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

I don't understand why the sortedArray works everywhere but in this one method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪恋 2024-09-17 09:11:26

来自 sortedArrayUsingSelector: 的文档:

新数组包含对接收者元素的引用,而不是它们的副本。

也许原来的琴弦已经发布了?

顺便说一句,我发现你没有释放 studentNames - 内存泄漏......

From documentation of sortedArrayUsingSelector::

The new array contains references to the receiver’s elements, not copies of them.

Maybe the original strings are already released?

BTW, I see that you don't release the studentNames - memory leak...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文