void *t = (void *)self; // ERROR: Cast of ... requires a bridged cast void *t = (__bridge void *)self; // CORRECT
void f(void *t) { .... } f((void *)self); // ERROR f((__bridge void *)self); // CORRECT
我认为这也适用于方法,事实上这个Beginning ARC in iOS 5 Tutorial给出了以下示例,并说需要__bridge:
MyClass *myObject = [[MyClass alloc] init]; [UIView beginAnimations:nil context:(__bridge void *)myObject];
但是,今天我不小心在我的一个程序中的方法调用中删除了一个__bridge,并且代码编译并运行没有任何问题.上面示例中的__bridge似乎是不必要的:
[UIView beginAnimations:nil context:(void *)myObject]; // COMPILED OK
解决方法
3.3.3 Conversion from retainable object pointer type in certain contexts
[beginning Apple 4.0,LLVM 3.1]
If an expression of retainable object pointer type is explicitly cast
to a C retainable pointer type,the program is ill-formed as discussed
above unless the result is immediately used:
在您的代码中,myObject是一个“可保留的对象指针”. “C可保留指针类型”包括void *(这是一个稍微草率的定义,它们用作占位符,因为Core Foundation“对象”通常是无效的*).
因此,如果用作方法参数,则可以将ObjC对象隐式转换为void *.在这种情况下,没有额外的内存管理语义(即它相当于__bridge强制转换). Section 7.8警告我们,将来可能不会以这种方式对待*,但我不担心.如果发生这种情况,添加__bridge将是微不足道的.
要记住的一件事是myObject在这里不受保护.您需要确保在动画完成之前保留其他方式,否则您可能会崩溃.
原文地址:https://www.jb51.cc/iOS/334466.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。