使用ARC将指针传递给NSArray内的对象

use*_*193 1 pointers bridge reference objective-c automatic-ref-counting

我正在转换为ARC,并且在没有破坏代码的情况下找不到任何方法来摆脱这个编译器错误.

我需要将2个对象传递给选择器,因此我将它们都添加到数组中,如下所示,并将该数组发送到选择器:

LWPagerContent* content = nil;
NSArray* args = [NSArray arrayWithObjects:article, [NSValue valueWithPointer:&content], nil];

[self performSelectorOnMainThread:@selector(initArticlePagerContent:) withObject:args waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

此方法访问数组,但需要在数组中引用的地址处修改对象.它应该如图所示初始化LWPagerContent对象,然后该对象将由调用performSelector的原始方法使用.

- (void)initArticlePagerContent:(NSArray*)args {
Article* article = [args objectAtIndex:0];

LWPagerContent** contentPtr = [[args objectAtIndex:1] pointerValue];
*contentPtr = [[NSClassFromString([StyleSheet articleContentClass]) alloc] initWithArticle:article];
Run Code Online (Sandbox Code Playgroud)

}

这工作正常,但当我尝试转换为ARC时,我立即被告知"Implicit conversion of a non-Objective-C type 'void *' to 'LWPagerContent *__strong *' is disallowed with ARC".在以"LWPagerContent **..."(上面显示的第二个到最后一个)开头的行上发生此错误.我尝试过使用__bridge__autoreleasing其他帖子中的推荐,但似乎没有任何效果.任何建议都非常感谢.

bbu*_*bum 7

使用块转到主线程,消除-performSelector:call暗示的元编程.

然后使用回调来设置生成的内容.

dispatch_sync(dispatch_get_main_queue(), ^{
    LWPagerContent *newContent = ... do something ...
    [self setPagerContent:newContent];
});
Run Code Online (Sandbox Code Playgroud)

一些说明:

  • init...除非它们是初始化程序,否则不应调用这些方法

  • 通常要避免通过引用

  • 元编程破坏了编译器进行健全性检查的能力.躲开它.