首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可可NSBrowser和CFURLEnumeratorCreateDirectoryURL

可可NSBrowser和CFURLEnumeratorCreateDirectoryURL
EN

Stack Overflow用户
提问于 2012-03-28 19:16:17
回答 1查看 627关注 0票数 0

我正在尝试理解苹果"ComplexBrowser“中的示例,但是很难找到任何关于"CFURLEnumeratorCreateDirectoryURL”的材料/教程。

苹果ComplexBrowser样品

这段代码到底是怎么回事?

我不明白用CFURLEnumeratorGetNextURL和其他东西循环的方式。

对我来说,使用NSFileManager的方法似乎更简单,但更有限?

NSArray *contentsAtPath = [NSFileManager defaultManager contentsOfDirectoryAtPath:parentPath错误:NULL];

代码语言:javascript
复制
- (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;

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-28 19:57:44

由于这些信息存储在不透明的C数据类型中,所以在核心基础上,它们提供了C例程,这些例程为您提供了有关数据的信息。这是一种封装形式,这样它们就可以在幕后更改内容,而不会影响库的公共接口。

基本上,他们创建一个循环,并一直从目录中请求下一个URL,直到找到目录的末尾。

  • enumerator是一种索引,可以跟踪它们在URL列表中的位置。
  • enumeratorResult告诉我们我们是否得到了一个新的URL (或者有一个错误,或者我们在最后一个记录中)。

当它遍历每个URL时,它会创建FileSystemNode并将它们添加到一个数组中,然后在完成遍历目录中的所有URL时返回该数组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9914203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档