Pass an Objective-C object to a function as a void * pointer

别等时光非礼了梦想. 提交于 2019-12-28 02:52:48

问题


I have a function:

  myFunction (MyProc callback, void * ref)

This function is called from within an Objective-C class. The function is passed a pointer to the callback (a function in the class) and a reference. The reference is necessary because the callback is called statically and therefore doesn't have a context. The ref can be used to provide a context to the callback.

I want to be able to pass the Objective-C class as the reference. So the question is:

How do I cast an NSObject to a void * and how do I cast a void * as an NSObject.

Thanks in advance.


回答1:


Do something like this:

void func(void *q)
{
    NSObject* o = CFBridgingRelease(q);
    NSLog(@"%@", o);
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSObject* o = [NSObject new];
        func((void*)CFBridgingRetain(o));
    }
    return 0;
}

Note that CFBridgingRetain() and CFBridgingRelease() are macros around compiler attributes. Feel free to use either. I like the API variant as it is in more common use in our codebases and it is more explicitly / less confusing.

CFBridgingRetain() effectively hard-retains the object that must be balanced by a CFBridgingRelease(). It also happens to return a CFTypeRef which is compatible with a cast to void*. CFBridgingRelease() effectively undoes that hard-retain and, thus, q will only remain valid within the scope that o is valid.

Valid for basic callbacks, but you'd probably not what that with a void *context; type thing that has to stick around for a while. For that:

void callback(void *context)
{
    // grab an ARC aware reference without impacting hard-retain
    NSObject* o = (__bridge NSObject *)(context);
    NSLog(@"%@", o);
}

void freeContext(void *context)
{
    // release the hard-retain
    CFBridgingRelease(context);
}

Note that Xcode is quite good about suggesting exactly what you should do if you leave out the cast / API call. It even explains the meanings of each of the alternative solutions (I relied on this heavily until I could keep 'em straight in my head).




回答2:


I assume you are using ARC. You can use something like this when calling myFunction

id ref = ...; // your Objective-C object
myFunction(callback, (__bridge_retained void *) ref);

In your callback, you must transfer the ownership back:

void callback(void* refPtr) {
    id refObj = (__bridge_transfer id) refPtr;
}

Replace id with your object type as appropriated.




回答3:


Answer: in no way. void * is implicitly compatible with any pointer type, so if you have an object, which is a pointer of type id (alias for struct objc_object *), you can simply pass it where the void pointer is needed, without casting. Example:

// this is the declaration of the callback function:
void callback(void *context);

// then you can call it like this:
SomeClass *obj = [[SomeClass alloc] init];
callback(obj);


来源:https://stackoverflow.com/questions/12916491/pass-an-objective-c-object-to-a-function-as-a-void-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!