2011-07-28 3 views
4

NSArrayのようなツリークラスをCFTreeにしたいと思っています。 (実際にはNSTreeは存在しないので、 'NSTree like tree'を作成しようとしています。)iphone - cftreeでツリークラスを作る

しかし、CFTreeは特定のタイプのクラスを要求しているようです。 NSArrayのようなさまざまなクラスで扱える汎用クラスを作りたいです。

ここはiPhone開発サイトの例です。

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のインスタンスを保存する場合は、コンテキストのretainreleaseCFRetainCFReleaseに設定します。 copyDescriptionCFCopyDescriptionに設定することもできます。このようなもの:

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); 
関連する問題