【发布时间】:2011-07-06 10:17:56
【问题描述】:
我有一些 Objective-C 代码可以为我完成这项工作。但它丑陋(低效)吗?使用 for-each 循环可以更好地执行吗?
请看这段代码:
for (int i = 0; i < [careerIds count]; i++) {
NSString *titleString = [[titles objectAtIndex:i] stringValue];
if ([titleString isEqualToString:@""] || [titleString rangeOfString:@"Intresseanmälan"].location != NSNotFound) {
// Don't add the id
} else {
[ids addObject:[[careerIds objectAtIndex:i] stringValue]];
}
}
【问题讨论】:
-
可能不会。这有点难看,但我认为在这种情况下担心效率可能是错误的。
-
如果您担心速度,请参阅this answer @Deepak 实际上对各种选项进行计时,并发现不使用快速迭代比使用快速迭代更快,如果这意味着您还必须
-indexOfObject。或者至少在案例中经过测试。
标签: objective-c performance for-loop