ios – 方法参数不需要__bridge?

我们知道,使用ARC我们需要一个__bridge来将id转换为void *:
void *t = (void *)self;           // ERROR: Cast of ... requires a bridged cast
void *t = (__bridge void *)self;  // CORRECT

C函数调用也是如此:

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

这是正确的吗?在这种情况下,__bridge真的没用吗?或者删除它会改变代码的含义?

解决方法

这在 ARC docs section 3.3.3(强调我的)中有所涉及:

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:

  • to initialize a parameter in an Objective-C message send where the
    parameter is not marked with the cf_consumed attribute,or to
  • initialize a parameter in a direct call to an audited function where
    the parameter is not marked with the cf_consumed attribute.

在您的代码中,myObject是一个“可保留的对象指针”. “C可保留指针类型”包括void *(这是一个稍微草率的定义,它们用作占位符,因为Core Foundation“对象”通常是无效的*).

因此,如果用作方法参数,则可以将ObjC对象隐式转换为void *.在这种情况下,没有额外的内存管理语义(即它相当于__bridge强制转换). Section 7.8警告我们,将来可能不会以这种方式对待*,但我不担心.如果发生这种情况,添加__bridge将是微不足道的.

要记住的一件事是myObject在这里不受保护.您需要确保在动画完成之前保留其他方式,否则您可能会崩溃.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...