for(UIView *view in [_scrollView subviews]) {
NSMutableArray *_mutableArray = [_array mutableCopy];
[_mutableArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"guid like %@",view.object.guid]];
if([_mutableArray count] != 0) {
//object is already on screen we need to update it
[view setObject:(Object*)[_mutableArray objectAtIndex:0]];
}
else {
//object not on screen add it
_mutableArray = [_array mutableCopy];
[_mutableArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"guid not like %@",view.object.guid]];
UIView *newView = [[UIView alloc] initWithObject:(Object*)[_mutableArray objectAtIndex:0]];
[_scrollView addSubview:newView];
}
}
正如您所看到的,此方法更新屏幕上的对象,并且如果某个对象不在屏幕上,它将被创建并添加到scrollView子视图层次结构中。 现在我担心的是,我正在修改scrollView子视图层次结构,同时枚举它。这会最终崩溃我的应用程序吗?这段代码是否会随机崩溃我的应用程序?
如果是,我如何修改上面的代码以保证安全并实现相同的功能?
谢谢!
你使用ARC吗?似乎你有一个与内存管理有关的问题 – NeverBe 2012-02-09 22:12:38
我正在使用ARC。代码没有崩溃,但我不确定它不会在将来。这种添加子视图到scrollView(枚举它)的方式是一种有效的方式吗? – 2012-02-09 22:32:10
你在_array存储什么? – NeverBe 2012-02-09 22:37:31