我正在爲一個相當複雜的表實現一個自定義的searchBar,並且再次遇到了這種代碼模式。這從一開始就iPhone開發的書樣本:快速枚舉(循環)在Objective-C中如何工作? (即:for(NSString * aString in a Dictionary))
- (void)handleSearchForTerm:(NSString *)searchTerm
{
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];
for (NSString *key in self.keys)
{
NSMutableArray *array = [self.names valueForKey:key];
NSMutableArray *toRemove = [[NSMutableArray alloc] init];
for (NSString *name in array)
{
if ([name rangeOfString:searchTerm
options:NSCaseInsensitiveSearch].location == NSNotFound)
[toRemove addObject:name];
}
if ([array count] == [toRemove count])
[sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
[toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}
我很好奇的部分是參考「(的NSString *數組名)」。這是幹什麼的?它似乎爲數組中的每個項目創建一個字符串。另外,這是如何與字典工作?
謝謝!
感謝大家的快速反應!我編輯標題以包含「快速枚舉」。 – Jonah 2009-08-21 20:50:29