iphone - 使用 cftree 创建一个树类

标签 iphone objective-c ios core-foundation

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

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

谢谢。

最佳答案

当然,CFTree 可以保存您想要的任何数据。如果你想存储 CFType 的实例,那么只需将上下文的 retainrelease 设置为 CFRetain 并且CFRelease。然后,您还可以将 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);

关于iphone - 使用 cftree 创建一个树类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6856728/

相关文章:

iphone - 如何在 viewController 中以编程方式布局 View 大小/位置并顺利处理旋转

iphone - 当点击屏幕的另一部分时隐藏自定义 iPhone 控件

objective-c - 关闭两个模态视图 Controller

ios - cellForItemAtIndexPath : firing in iOS7 but not iOS6

ios - 如何使用 UIActionSheet 确认删除 UITableViewCell

objective-c - 我可以将 segues 与 View Controller 的指定初始化程序一起使用吗?

iphone - ScrollView 中的选择器不滚动

iphone - 如何通过使用 UIPanGestureRecognizer 平移 UIView 在 iPhone 中获得更多 320 的值

ios - 从特定值开始对字典数组进行排序

ios - 使用 RevenueCat 的应用在审核中被拒绝