在特定代码行上禁用 ARC

Disable ARC on specific lines of code

我正在将一个项目转换为 ARC,但有些代码只能在禁用 ARC 的情况下使用。我意识到 -fno-objc-arc 可用于在每个文件的基础上禁用 ARC。但是我想知道是否可以在每个函数的基础上禁用 ARC。

我知道可以在每行的基础上切换警告,例如

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
// Code goes here
#pragma clang diagnostic pop

ARC 有类似的东西吗? (以下是我的假设)

#pragma clang diagnostic push
#pragma clang diagnostic flag "-fno-objc-arc"
// Code goes here
#pragma clang diagnostic pop

这是不可能的。您必须将非 ARC 代码移动到单独的文件中。如果这是 Obj-C 中的代码 class,也许您可​​以将有问题的方法移到一个类别中。

编辑:

使用Objective-C++可以绕过Obj-C中ARC的一些限制。例如,您可以将 Obj-C 引用放在结构中。 (因为它们可以在 C++ 中有反初始化器)

编辑:

这段代码对我有用:

test.h

#import <Foundation/Foundation.h>

struct Struct {
    id ref ;
    NSDate * date ;
};

main.mm

#import "test.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        Struct a ;
        a.ref = [ NSObject new ] ;

        Struct * a2 = new Struct() ;
        a2->ref = [ NSObject new ] ;
        free( a2 ) ;
    }

    return 0;
}