Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if my program has ARC enabled or not?

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?

I am using XCode 4.2.1 Build 4D502

like image 294
user705414 Avatar asked Jul 08 '12 03:07

user705414


4 Answers

You can use __has_feature, maybe logging whether the project has ARC in the console like this:

#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:

#if  ! __has_feature(objc_arc)
    #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
like image 132
pasawaya Avatar answered Oct 11 '22 15:10

pasawaya


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.

However, if you want to know where in Xcode the setting lives:

  1. Select your project in the navigator. This will open the project editor.
  2. In the project editor, select your target.
  3. In the project editor, click Build Settings (if it isn't already selected).
  4. In the project editor, click the search box and type 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.

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. :)

To see which files override the ARC setting:

  1. Select your project in the navigator. This will open the project editor.
  2. In the project editor, select your target.
  3. In the project editor, click Build Phases (not Build Settings).
  4. Expand the Compile Sources build phase.

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.

like image 33
Steven Fisher Avatar answered Oct 11 '22 15:10

Steven Fisher


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.

like image 28
jpalten Avatar answered Oct 11 '22 14:10

jpalten


add "-fno-objc-arc" compiler flags for all the .m files in build phases

like image 42
holybiner Avatar answered Oct 11 '22 13:10

holybiner