如何获取一个客观的c对象的地址到一个void * volatile *下ARC? [英] How to get the address of an objective c object into a void * volatile * under ARC?

查看:578
本文介绍了如何获取一个客观的c对象的地址到一个void * volatile *下ARC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的目标c对象



NSManagedObjectContext * moc = nil



现在我需要将它传递给接受类型

的参数的ARC环境中的函数。

void * volatile * value



我尝试了



(__bridge void *)moc))



但我得到以下编译器错误



地址表达式必须是左值或函数指针



我也试过了

  void ** addr =(__bridge void **)(& moc); 

但我得到错误



code>不兼容的类型使用__bridge转换将'NSManagedObjectContext * __strong *'转换为'void **'





>更具体地说,我试图实现另一个 stackoverflow问题中描述的单例模式,但我有在ARC环境中,将目标c NSManagedObjectContext 对象的地址馈送到以下函数的第三个参数中。



OSAtomicCompareAndSwapPtrBarrier(void * __ oldValue,void * __ newValue,void * volatile * __ theValue)

解决方案

另一个现代单形模式我在现代时代看到/使用了很多使用GCD的dispatch_once。你直接询问ARC的指针转换(以及下面的内容),但是为了满足你的实际需要,为什么不用GCD做这个呢?像这样:

  +(NSFoo *)sharedFoo 
{
static dispatch_once_t pred;
static NSFoo * sFoo;
dispatch_once(& pred,^ {sFoo = [[NSFoo alloc] init];});
return sFoo;
}

正如你可以看到 dispatch_once的源代码,GCD实现了你想在这里实现的相同模式。 GCD通过不使用ARC跟踪指针作为CompareAndSwaps的东西,而是使用代理值(这里 pred )解决了您遇到的问题,因此即使你有一些厌恶使用GCD为你的单身,你可以考虑模仿他们的方法和使用代理。



也就是说,如果你只是使用GCD为你的case,你不必担心像gnarly ARC指针转换的东西。您还可以合理地确保GCD实现在多个平台/架构(MacOS / iOS)上表现一致。


I have a simple objective c object

NSManagedObjectContext * moc = nil

Now I need to pass it into a function in an ARC environment that accepts parameter of type

void *volatile * value

I tried

&((__bridge void *)moc))

but I get the following compiler error

Address expression must be lvalue or a function pointer

I also tried

void ** addr = (__bridge void **)(&moc);

But I get the error

Incompatible types casting 'NSManagedObjectContext * __strong *' to 'void **' with a __bridge cast

Is there any way to first cast and then get the address of the casted pointer?

EDIT

To be more specific, I'm trying to implement the singleton pattern described in another stackoverflow question, but I'm having trouble feeding the address of the objective c NSManagedObjectContext object into the third argument of the following function in an ARC environment.

OSAtomicCompareAndSwapPtrBarrier(void *__oldValue, void *__newValue, void *volatile *__theValue)

解决方案

Another modern singleton pattern I've been seeing/using a lot in the modern era uses GCD's dispatch_once. You asked directly about pointer casting for ARC (and more on that below), but in terms of filling your actual need, why not just do this with GCD? Like this:

+ (NSFoo*)sharedFoo
{
    static dispatch_once_t pred;
    static NSFoo* sFoo;
    dispatch_once(&pred, ^{ sFoo = [[NSFoo alloc] init]; } );
    return sFoo;
}

As you can see if you go look at the source for dispatch_once, GCD implements the same pattern you're seeking to implement here. GCD gets around the problem you're having by not using an ARC-tracked pointer as the thing it CompareAndSwaps, but rather using a surrogate value (here pred), so even if you have some aversion to using GCD for your singleton, you might consider mimicking their approach and using a surrogate.

That said, if you just use GCD for your case, you don't ever have to worry about stuff like gnarly ARC pointer casting. You can also be reasonably sure that the GCD implementation will behave consistently across multiple platforms/architectures (MacOS/iOS).

这篇关于如何获取一个客观的c对象的地址到一个void * volatile *下ARC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆