2011-07-28 34 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);