在特定代码行上禁用ARC [英] Disable ARC on specific lines of code
问题描述
我把一个项目变成ARC,但有一些代码只能使用ARC禁用。我意识到 -fno-objc-arc 可用于在每个文件的基础上禁用ARC。但是我想知道是否可以在每个函数的基础上禁用ARC。
I am turning a project into ARC, but there are some bits of code that can only work with ARC disabled. I realize that -fno-objc-arc can be used for disabling ARC on a per file basis. However I was wondering if it was possible to disable ARC on a per function basis.
我知道警告可以在每行基础上切换,例如
I know that warnings can be toggled on a per line basis for example
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
// Code goes here
#pragma clang diagnostic pop
ARC有类似的东西吗? (以下是我的假设想法)
Is there something similar for ARC? (Below is my hypothetical idea)
#pragma clang diagnostic push
#pragma clang diagnostic flag "-fno-objc-arc"
// Code goes here
#pragma clang diagnostic pop
推荐答案
这是不可能的。您必须将非ARC代码移动到单独的文件。如果这是Obj-C类中的代码,也许你可以将相关方法移动到一个类别。
This isn't possible. You'll have to move your non-ARC code to a separate file. If this is code in an Obj-C class, perhaps you could move the methods in question to a category.
编辑:
使用Objective-C ++可以解决Obj-C中ARC的一些限制。例如,你可以在结构体中放置Obj-C引用。 (因为他们可以在C ++中有deinitializers)
Using Objective-C++ can get around some of the restrictions of ARC in Obj-C. For example, you can put Obj-C references in structs. (Because they can have deinitializers in 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;
}
这篇关于在特定代码行上禁用ARC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!