在 UIPickerView 的 numberOfRowsInComponent 中返回 [anArray count] 时出错

发布于 2024-11-27 02:47:52 字数 1855 浏览 1 评论 0原文

我有一个 UIViewController,它有一个 UIPickerView 和 UITableView。 UIPicker 有 3 个组件。

当我尝试通过返回 [anArray count] 来定义每个数组中的组件数量时,程序会冻结,但在加载 UIViewController 时不会抛出错误。

当我将 NSLogs 放入 viewDidLoad: 中时,[self.hours count][self.minutes count][self. meridiem count],返回正确的值数量。

- (void)viewDidLoad {

[super viewDidLoad];

NSMutableArray *tempHours = [NSMutableArray array];
for (NSInteger i = 0; i < 12; i++) [tempHours addObject:[NSNumber numberWithInteger:(i+1)]];
self.hours = [NSArray array];
hours = tempHours;
[tempHours release];

NSMutableArray *tempMinutes = [NSMutableArray array];
for (NSInteger i = 0; i <= 59; i++) [tempMinutes addObject:[NSNumber numberWithInteger:i]];
self.minutes = [NSArray array];
minutes = tempMinutes;
[tempMinutes release];

NSMutableArray *tempMeridiem = [NSMutableArray array];
for (NSInteger i = 0; i <= 1; i++) [tempMeridiem addObject:[NSNumber numberWithInteger:i]];
self.meridiem = [NSArray array];
meridiem = tempMeridiem;
[tempMeridiem release];

}

现在,在 pickerView:numberOfRowsInComponent:Component: 中,如果我尝试输出 [anArray count](其中 anArray 是小时、分钟的占位符,和子午线)。如果我将 numberOfRows 设置为整数值或 component+1 ,一切都会正常工作。

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

NSInteger numberOfRows;

if (component == 0) {
    // numberOfRows = component+1;
    numberOfRows = [self.hours count];
}
else if(component == 1) { 
    // numberOfRows = component+1;
    numberOfRows = [self.minutes count];
}
else { 
    // numberOfRows = component+1;
    numberOfRows = [self.meridiem count];
}

return numberOfRows;

}

我有一种感觉,我的 NSArray(小时、分钟、子午线)没有被保留,但是运行分析器并没有显示我在该 ViewController 中存在任何内存泄漏。

任何和所有的帮助将不胜感激!

I have a UIViewController that has a UIPickerView and UITableView.
The UIPicker has 3 components.

When I try to define the number of components in each array by returning [anArray count], the program freezes without throwing an error when loading the UIViewController.

When I put NSLogs in viewDidLoad: for [self.hours count], [self.minutes count], and [self.meridiem count], the correct number of values are returned.

- (void)viewDidLoad {

[super viewDidLoad];

NSMutableArray *tempHours = [NSMutableArray array];
for (NSInteger i = 0; i < 12; i++) [tempHours addObject:[NSNumber numberWithInteger:(i+1)]];
self.hours = [NSArray array];
hours = tempHours;
[tempHours release];

NSMutableArray *tempMinutes = [NSMutableArray array];
for (NSInteger i = 0; i <= 59; i++) [tempMinutes addObject:[NSNumber numberWithInteger:i]];
self.minutes = [NSArray array];
minutes = tempMinutes;
[tempMinutes release];

NSMutableArray *tempMeridiem = [NSMutableArray array];
for (NSInteger i = 0; i <= 1; i++) [tempMeridiem addObject:[NSNumber numberWithInteger:i]];
self.meridiem = [NSArray array];
meridiem = tempMeridiem;
[tempMeridiem release];

}

Now, in pickerView:numberOfRowsInComponent:Component:, the code freezes without an error if I try to output [anArray count](where anArray is a placeholder for hours, minutes, and meridiem). If I set numberOfRows to an integer value, or to component+1 everything works just fine.

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

NSInteger numberOfRows;

if (component == 0) {
    // numberOfRows = component+1;
    numberOfRows = [self.hours count];
}
else if(component == 1) { 
    // numberOfRows = component+1;
    numberOfRows = [self.minutes count];
}
else { 
    // numberOfRows = component+1;
    numberOfRows = [self.meridiem count];
}

return numberOfRows;

}

I have a feeling my NSArrays (hours, minutes, meridiem) aren't being retained, but running the Analyzer doesn't show me that I have any memory leaks in that ViewController.

Any and all help would be greatly appreciated!

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

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

发布评论

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

评论(3

瞄了个咪的 2024-12-04 02:47:52

您正在创建不必要的数组,并且没有保留重要的数组。查看您的代码:

self.hours = [NSArray array]; 
hours = tempHours; 
[tempHours release];

在这里,您创建了一个自动释放的数组并将其设置为 self.hours 属性(希望附加一个 retain 关键字)。但接下来,您直接访问 ivar 并将其设置为 tempHours。然后,您向 tempHours 发送一条释放消息,而无需保留它。由于 tempHours 是自动释放的,它会在自动释放池的下一次耗尽时神奇地消失。我相信你想做的是:

self.hours = tempHours;

你的其他数组集合也是如此。

You are creating unnecessary arrays and not retaining the ones that count. See your code:

self.hours = [NSArray array]; 
hours = tempHours; 
[tempHours release];

Here you created an autoreleased array and set it to your self.hours property (which hopefully has a retain keyword attached to it). But then the very next line you access the ivar directly and set it to tempHours. Then you send a release message to tempHours without having ever retained it. Since tempHours is autoreleased it will magically disappear on the next drain of the auto release pool. I believe what you wanted to do is:

self.hours = tempHours;

Ditto for your other array collections.

岁月无声 2024-12-04 02:47:52

这些行是错误的:

self.hours = [NSArray array];
hours = tempHours;

您应该只使用

self.hours = tempHours;

原始代码中发生的情况是:

  1. self.hours 设置为由 [NSArray array] 创建的数组。称之为aa 已正确保留。

  2. 现在,hours 设置为 tempHours。这直接访问 ivar,而不使用 setter。因此,它不会保留 tempHoursa 也会在未正确释放的情况下丢失。

  3. tempHours 已发布。它不被任何人保留,所以它也被释放。

所以,不要这样做。您想阅读 内存管理指南。我不敢相信分析器(在构建命令下方)没有发现这个错误...也许您想向 LLVM 团队提交有关此案例的错误。

These lines are wrong:

self.hours = [NSArray array];
hours = tempHours;

you should just use

self.hours = tempHours;

What was happening in your original code was:

  1. self.hours is set to an array created by [NSArray array]. Call it a. a is correctly retained.

  2. now, hours is set to tempHours. This directly accesses the ivar, without using the setter. So, it doesn't retain tempHours. a is lost without being correctly released, too.

  3. tempHours is released. It's not retained by anybody, so it's dealloc'ed, too.

So, don't do that. You'd like to read the memory management guide. I can't believe the Analyzer (below the Build command) didn't catch this mistake... maybe you'd like to file a bug toward the LLVM team about this case.

笑叹一世浮沉 2024-12-04 02:47:52

问题不在我试图显示的 ViewController 中。我只是将接口和实现文件复制到一个新项目中,编译后,一切都像做梦一样顺利。

问题出在我的项目的 RootViewController 上,它以 UIPickerView 的方式调用控制器。显然,我那里有内存泄漏。修复起来应该很简单。

感谢大家的帮助!

The problem was not in the ViewController I was trying to display. I just copied the interface and implementation files into a new proejct, and once I compiled it, everything worked like a dream.

The problem is with the RootViewController of my project that modally invokes the controller with the UIPickerView. Apparently, I have a memory leak in there. Should be trivial to fix.

Thanks everyone for your help!

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