【问题标题】:iphone - Make a tree class with cftreeiphone - 使用 cftree 创建一个树类
【发布时间】:2011-07-28 09:24:03
【问题描述】:

我想用 CFTree 创建一个像 NSArray 这样的树类。 (实际上 NSTree 并不存在,所以我正在尝试制作 'NSTree like tree'。)

但 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”。 有什么办法吗?

谢谢。

【问题讨论】:

    标签: iphone objective-c ios core-foundation


    【解决方案1】:

    当然,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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-21
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      相关资源
      最近更新 更多