I create a project with ARC support using the Xcode project wizard. Compared with a program without ARC support, I did not notice any differences. Is there any hint that can tell me if my program supports ARC?
我使用Xcode项目向导创建了一个支持ARC的项目。与没有ARC支持的程序相比,我没有发现任何差异。如果我的程序支持ARC,是否有任何提示可以告诉我?
I am using XCode 4.2.1 Build 4D502
我正在使用XCode 4.2.1 Build 4D502
4 个解决方案
#1
25
You can use __has_feature
, maybe logging whether the project has ARC in the console like this:
您可以使用__has_feature,也可以在控制台中记录项目是否具有ARC,如下所示:
#if __has_feature(objc_arc)
// ARC is On
NSLog(@"ARC on");
#else
// ARC is Off
NSLog(@"ARC off");
#endif
Alternatively, instead of just logging whether ARC is on, try making the compiler raise an error if ARC is on (or off) like this:
或者,不要只记录ARC是否打开,如果ARC打开(或关闭),请尝试使编译器引发错误,如下所示:
#if ! __has_feature(objc_arc)
#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
#2
18
If you just want to know one time if your project has ARC, I suggest using jpalten's answer. If you want your code to only build with ARC on, I suggest qegal's.
如果您只想知道您的项目是否有ARC,我建议使用jpalten的答案。如果您希望代码仅使用ARC构建,我建议使用qegal。
However, if you want to know where in Xcode the setting lives:
但是,如果您想知道Xcode在设置中的位置:
- Select your project in the navigator. This will open the project editor.
- 在导航器中选择您的项目。这将打开项目编辑器。
- In the project editor, select your target.
- 在项目编辑器中,选择目标。
- In the project editor, click Build Settings (if it isn't already selected).
- 在项目编辑器中,单击“构建设置”(如果尚未选中)。
- In the project editor, click the search box and type
OBJC_ARC
. - 在项目编辑器中,单击搜索框并键入OBJC_ARC。
This will leave a single line showing in Build Settings, either Objective-C Automatic Reference Counting or CLANG_ENABLE_OBJC_ARC (depending on whether you're showing setting descriptions or names). If it's set to YES, you have ARC on.
这将在Build Settings中显示一行,Objective-C Automatic Reference Counting或CLANG_ENABLE_OBJC_ARC(取决于您是否显示设置描述或名称)。如果设置为YES,则表示已启用ARC。
This is your target's pick of whether ARC is used. Note that each file can be compiled with a different setting. This is not what you asked, but since we already have three answers I thought I'd head off a possible fourth. :)
这是您选择是否使用ARC的目标。请注意,可以使用不同的设置编译每个文件。这不是你问的问题,但是因为我们已经有了三个答案,所以我认为我可能会排在第四位。 :)
To see which files override the ARC setting:
要查看哪些文件覆盖ARC设置:
- Select your project in the navigator. This will open the project editor.
- 在导航器中选择您的项目。这将打开项目编辑器。
- In the project editor, select your target.
- 在项目编辑器中,选择目标。
- In the project editor, click Build Phases (not Build Settings).
- 在项目编辑器中,单击“构建阶段”(而不是“构建设置”)。
- Expand the Compile Sources build phase.
- 展开Compile Sources构建阶段。
Here, you can see the compiler flags for each file. Files with that set whether or not to use ARC will include either -fno-objc-arc or -fobjc-arc, for specifying MRR or ARC respectively.
在这里,您可以看到每个文件的编译器标志。具有该设置的文件是否使用ARC将包括-fno-objc-arc或-fobjc-arc,分别用于指定MRR或ARC。
#3
5
Or to test just once, add this in your code:
或者只测试一次,在代码中添加:
NSString* dummy = [[[NSString alloc] init] autorelease];
This should raise an error if you are using ARC. If it does, ARC is enabled, all is fine and you can remove it again.
如果您使用ARC,这应该会引发错误。如果是,ARC已启用,一切正常,您可以再次删除它。
#4
1
add "-fno-objc-arc" compiler flags for all the .m files in build phases
在构建阶段为所有.m文件添加“-fno-objc-arc”编译器标志
#1
25
You can use __has_feature
, maybe logging whether the project has ARC in the console like this:
您可以使用__has_feature,也可以在控制台中记录项目是否具有ARC,如下所示:
#if __has_feature(objc_arc)
// ARC is On
NSLog(@"ARC on");
#else
// ARC is Off
NSLog(@"ARC off");
#endif
Alternatively, instead of just logging whether ARC is on, try making the compiler raise an error if ARC is on (or off) like this:
或者,不要只记录ARC是否打开,如果ARC打开(或关闭),请尝试使编译器引发错误,如下所示:
#if ! __has_feature(objc_arc)
#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
#2
18
If you just want to know one time if your project has ARC, I suggest using jpalten's answer. If you want your code to only build with ARC on, I suggest qegal's.
如果您只想知道您的项目是否有ARC,我建议使用jpalten的答案。如果您希望代码仅使用ARC构建,我建议使用qegal。
However, if you want to know where in Xcode the setting lives:
但是,如果您想知道Xcode在设置中的位置:
- Select your project in the navigator. This will open the project editor.
- 在导航器中选择您的项目。这将打开项目编辑器。
- In the project editor, select your target.
- 在项目编辑器中,选择目标。
- In the project editor, click Build Settings (if it isn't already selected).
- 在项目编辑器中,单击“构建设置”(如果尚未选中)。
- In the project editor, click the search box and type
OBJC_ARC
. - 在项目编辑器中,单击搜索框并键入OBJC_ARC。
This will leave a single line showing in Build Settings, either Objective-C Automatic Reference Counting or CLANG_ENABLE_OBJC_ARC (depending on whether you're showing setting descriptions or names). If it's set to YES, you have ARC on.
这将在Build Settings中显示一行,Objective-C Automatic Reference Counting或CLANG_ENABLE_OBJC_ARC(取决于您是否显示设置描述或名称)。如果设置为YES,则表示已启用ARC。
This is your target's pick of whether ARC is used. Note that each file can be compiled with a different setting. This is not what you asked, but since we already have three answers I thought I'd head off a possible fourth. :)
这是您选择是否使用ARC的目标。请注意,可以使用不同的设置编译每个文件。这不是你问的问题,但是因为我们已经有了三个答案,所以我认为我可能会排在第四位。 :)
To see which files override the ARC setting:
要查看哪些文件覆盖ARC设置:
- Select your project in the navigator. This will open the project editor.
- 在导航器中选择您的项目。这将打开项目编辑器。
- In the project editor, select your target.
- 在项目编辑器中,选择目标。
- In the project editor, click Build Phases (not Build Settings).
- 在项目编辑器中,单击“构建阶段”(而不是“构建设置”)。
- Expand the Compile Sources build phase.
- 展开Compile Sources构建阶段。
Here, you can see the compiler flags for each file. Files with that set whether or not to use ARC will include either -fno-objc-arc or -fobjc-arc, for specifying MRR or ARC respectively.
在这里,您可以看到每个文件的编译器标志。具有该设置的文件是否使用ARC将包括-fno-objc-arc或-fobjc-arc,分别用于指定MRR或ARC。
#3
5
Or to test just once, add this in your code:
或者只测试一次,在代码中添加:
NSString* dummy = [[[NSString alloc] init] autorelease];
This should raise an error if you are using ARC. If it does, ARC is enabled, all is fine and you can remove it again.
如果您使用ARC,这应该会引发错误。如果是,ARC已启用,一切正常,您可以再次删除它。
#4
1
add "-fno-objc-arc" compiler flags for all the .m files in build phases
在构建阶段为所有.m文件添加“-fno-objc-arc”编译器标志