在 UIPickerView 的 numberOfRowsInComponent 中返回 [anArray count] 时出错
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在创建不必要的数组,并且没有保留重要的数组。查看您的代码:
在这里,您创建了一个自动释放的数组并将其设置为 self.hours 属性(希望附加一个
retain
关键字)。但接下来,您直接访问 ivar 并将其设置为 tempHours。然后,您向tempHours
发送一条释放消息,而无需保留它。由于 tempHours 是自动释放的,它会在自动释放池的下一次耗尽时神奇地消失。我相信你想做的是:你的其他数组集合也是如此。
You are creating unnecessary arrays and not retaining the ones that count. See your code:
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 totempHours
. Then you send a release message totempHours
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:Ditto for your other array collections.
这些行是错误的:
您应该只使用
原始代码中发生的情况是:
self.hours
设置为由[NSArray array]
创建的数组。称之为a
。a
已正确保留。现在,
hours
设置为tempHours
。这直接访问 ivar,而不使用 setter。因此,它不会保留tempHours
。a
也会在未正确释放的情况下丢失。tempHours
已发布。它不被任何人保留,所以它也被释放。所以,不要这样做。您想阅读 内存管理指南。我不敢相信分析器(在构建命令下方)没有发现这个错误...也许您想向 LLVM 团队提交有关此案例的错误。
These lines are wrong:
you should just use
What was happening in your original code was:
self.hours
is set to an array created by[NSArray array]
. Call ita
.a
is correctly retained.now,
hours
is set totempHours
. This directly accesses the ivar, without using the setter. So, it doesn't retaintempHours
.a
is lost without being correctly released, too.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.
问题不在我试图显示的 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!