Pregunta

I am trying to make a CFTree that holds strings as nodes. I am having trouble understanding the documentation for the class. Here is the link: https://developer.apple.com/library/mac/documentation/corefoundation/Reference/CFTreeRef/Reference/reference.html#//apple_ref/c/func/CFTreeSetContext

When you create a CFTree you call CFTreeCreate and pass in an allocator and a context. The allocator parameter I understand, but I don't understand the context argument.

Here are the fields you need to fill in in a context before you pass the context to CFTreeCreate:

version - this one I understand, I just make it version 0

info - Will I just set this to the NSString? Isn't that not OK because I need to use the __bridge or something? I'm kind of confused about this one

retain - Does ARC take care of retaining even though this is a Core Foundation class? I'm not sure what to put here

release - same as above

copyDescription - Not sure what the point of this field is or if I need it for what I'm trying to do.

¿Fue útil?

Solución

Will I just set this to the NSString? Isn't that not OK because I need to use the __bridge or something?

If you want to do that, you'll have to do a non retaining bridging cast. Because NSString is toll free bridged with CFStringRef, in the tree, you can treat your NString as a CFStringRef and set the retain and release functions to CFRetain and CFRelease respectively - I think it should be OK, the prototypes look compatible.

Does ARC take care of retaining even though this is a Core Foundation class?

No, it doesn't, but if you set the retain and release functions as I suggested, you should be OK.

copyDescription - Not sure what the point of this field is or if I need it for what I'm trying to do

It just spits out a description, in the same way as -description does for a Cocoa object. You can leave it null, but if your info is an NSString something like this will do the trick:

CFStringRef* copyDescription(void* info)
{
    return CFStringCreateCopy(someAllocator, (CFStringRef) info);
}

I haven't tried it, so it might not work.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top