我想了解來自Apple「ComplexBrowser」的示例,但它很難找到任何材料/教程「CFURLEnumeratorCreateDirectoryURL」。可可NSBrowser和CFURLEnumeratorCreateDirectoryURL
ComplexBrowser Sample from Apple
究竟在這段代碼是怎麼回事?
我不明白這種循環CFURLEnumeratorGetNextURL和東西的方式。
對我來說,NSFileManager的方法似乎更簡單,但更有限?
NSArray *contentsAtPath = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentPath error:NULL];
- (NSArray *)children {
if (_children == nil || _childrenDirty) {
// This logic keeps the same pointers around, if possible.
NSMutableArray *newChildren = [NSMutableArray array];
CFURLEnumeratorRef enumerator = CFURLEnumeratorCreateForDirectoryURL(NULL, (CFURLRef) _url, kCFURLEnumeratorSkipInvisibles, (CFArrayRef) [NSArray array]);
NSURL *childURL = nil;
CFURLEnumeratorResult enumeratorResult;
do {
enumeratorResult = CFURLEnumeratorGetNextURL(enumerator, (CFURLRef *) &childURL, NULL);
if (enumeratorResult == kCFURLEnumeratorSuccess) {
FileSystemNode *node = [[[FileSystemNode alloc] initWithURL:childURL] autorelease];
if (_children != nil) {
NSInteger oldIndex = [_children indexOfObject:childURL];
if (oldIndex != NSNotFound) {
// Use the same pointer value, if possible
node = [_children objectAtIndex:oldIndex];
}
}
[newChildren addObject:node];
} else if (enumeratorResult == kCFURLEnumeratorError) {
// A possible enhancement would be to present error-based items to the user.
}
} while (enumeratorResult != kCFURLEnumeratorEnd);
[_children release];
_childrenDirty = NO;
// Now sort them
_children = [[newChildren sortedArrayUsingComparator:^(id obj1, id obj2) {
NSString *objName = [obj1 displayName];
NSString *obj2Name = [obj2 displayName];
NSComparisonResult result = [objName compare:obj2Name options:NSNumericSearch | NSCaseInsensitiveSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch range:NSMakeRange(0, [objName length]) locale:[NSLocale currentLocale]];
return result;
}] retain];
}
return _children;
}
歡迎來到Stack Overflow(SO)!請點擊問題旁邊的箭頭接受幫助您的答案。因此告訴別人哪個答案是正確的,並且感謝那些給予他們聲望點的幫助你的人! – lnafziger 2012-03-30 20:59:01