我正在尝试理解苹果"ComplexBrowser“中的示例,但是很难找到任何关于"CFURLEnumeratorCreateDirectoryURL”的材料/教程。
苹果ComplexBrowser样品
这段代码到底是怎么回事?
我不明白用CFURLEnumeratorGetNextURL和其他东西循环的方式。
对我来说,使用NSFileManager的方法似乎更简单,但更有限?
NSArray *contentsAtPath = [NSFileManager defaultManager contentsOfDirectoryAtPath:parentPath错误: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;}
发布于 2012-03-28 19:57:44
由于这些信息存储在不透明的C数据类型中,所以在核心基础上,它们提供了C例程,这些例程为您提供了有关数据的信息。这是一种封装形式,这样它们就可以在幕后更改内容,而不会影响库的公共接口。
基本上,他们创建一个循环,并一直从目录中请求下一个URL,直到找到目录的末尾。
enumerator是一种索引,可以跟踪它们在URL列表中的位置。enumeratorResult告诉我们我们是否得到了一个新的URL (或者有一个错误,或者我们在最后一个记录中)。当它遍历每个URL时,它会创建FileSystemNode并将它们添加到一个数组中,然后在完成遍历目录中的所有URL时返回该数组。
https://stackoverflow.com/questions/9914203
复制相似问题