【发布时间】:2011-04-22 09:18:23
【问题描述】:
从实验看来,集合表达式只计算一次。考虑这个例子:
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) {
想法?
【问题讨论】:
-
感谢那些,特别是。试图回答的史穆克……这确实对此事有所启发。 (检查实现以澄清规范是危险的。无论如何,我在准备问题时已经这样做了。)
标签: iphone objective-c foreach fast-enumeration