从实验看来,集合表达式只被评估一次。考虑这个例子:
static NSArray *a;
- (NSArray *)fcn
{
if (a == nil)
a = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
NSLog(@"called");
return a;
}
...
for (NSString *s in [self fcn])
NSLog(@"%@", s);
输出是:
2010-10-07 07:37:31.419 WidePhotoViewer Lite[23694:207] called
2010-10-07 07:37:31.420 WidePhotoViewer Lite[23694:207] one
2010-10-07 07:37:31.425 WidePhotoViewer Lite[23694:207] two
2010-10-07 07:37:31.425 WidePhotoViewer Lite[23694:207] three
表示 [self fcn] 只被调用一次。
任何人都可以确认这是指定的(而不是仅仅观察到的)行为吗?
我想到的是做这样的事情:
for (UIView *v in [innerView subviews]) {
而不是这个:
NSArray *vs = [innerView subviews];
for (UIView *v in vs) {
想法?