2011-07-28 35 views
4

我想用CFTree创建一个像NSArray一样的树类。 (其实NSTree不存在,所以我试图使'NSTree像树')。iphone - 用cftree创建树类

但CFTree似乎要求某些类型的类。 我想做一个普通的类,可以处理像NSArray的各种类。

这是iPhone dev网站的例子。

static CFTreeRef CreateMyTree(CFAllocatorRef allocator) { 
    MyTreeInfo *info; 
    CFTreeContext ctx; 

    info = CFAllocatorAllocate(allocator, sizeof(MyTreeInfo), 0); 
    info->address = 0; 
    info->symbol = NULL; 
    info->countCurrent = 0; 
    info->countTotal = 0; 

    ctx.version = 0; 
    ctx.info = info; 
    ctx.retain = AllocTreeInfo; 
    ctx.release = FreeTreeInfo; 
    ctx.copyDescription = NULL; 

    return CFTreeCreate(allocator, &ctx); 
} 

我想使用通用类而不是“MyTreeInfo”。 有什么办法吗?

谢谢。

回答

3

当然,CFTree可以容纳任何你想要的数据。如果要存储CFType的实例,则只需将上下文的retainrelease设置为CFRetainCFRelease。您也可以将copyDescription设置为CFCopyDescription。类似这样的:

static CFTreeRef CreateMyTree(CFTypeRef rootObject) { 
    CFTreeContext ctx; 

    ctx.version = 0; 
    ctx.info = rootObject; 
    ctx.retain = CFRetain; 
    ctx.release = CFRelease; 
    ctx.copyDescription = CFCopyDescription; 

    return CFTreeCreate(NULL, &ctx); 
} 

... 

CFStringRef string = CFSTR("foo"); 
CFTreeRef tree = CreateMyTree(string); 
NSLog(@"%@", tree); 
CFRelease(tree);