How to Enable Arc Project-Wide in Xcode 4.2

How do you enable ARC project-wide in Xcode 4.2

"ARC is available in Xcode 4.2, currently in beta, and only when
compiling with Clang (a.k.a. "Apple LLVM compiler"). The setting is
called, obviously enough, "Objective-C Automatic Reference Counting".
Turn it on, and off you go.

If you're working on existing code, changing this setting will produce
an enormous quantity of errors. ARC not only manages memory for you,
but it forbids you from trying to do it yourself. It's illegal to
manually send retain/release/autorelease when using ARC. Since normal
non-ARC Cocoa code is littered with this stuff, you'll get a lot of
errors.

Fortunately, Xcode offers a tool to convert existing code. Select Edit
-> Refactor... -> Convert to Objective-C ARC
... and Xcode will guide you through converting your code. Although there may be some
situations where it needs help figuring out what to do, the process
should be largely automatic."

I took that from this link, helped me a lot:
http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html

How to enable/ disable Automatic Reference Counting?

Globally:

Go to "Build Settings", look for "Apple LLVM compiler 3.0 - Language". Set the field
"Objective-C Automatic Reference Counting" to "No".

Sample Image

For individual files:

Go to "Build Phases", select the file, double-click the "Compiler Flags" column and put
"-fno-objc-arc" in it.

How to change to use ARC from nonARC project?

This screen shot will help you

Sample Image

Read this DaveOnCode and Migrating your code to Objective-C ARC.

Reference code in a separate project in XCode 4.2

So I figured it out myself, with a lot of help from different blogs. Something this basic should be more trivial and well documented... But here we go, this is what I did to get a library for AsiHttpRequest:

  1. Create a new iOS project. Select the 'Cocoa Touch Static Library' template. Call it whatever you like. You don't want to tick 'Use automatic reference counting', since AsiHttpRequest does not support it.
  2. Select a location for your library project (will matter later on).
  3. Delete the default .h- and .m-file created by Xcode.
  4. Drag and drop the AsiHttpRequest files into the project
  5. You can add the frameworks that AsiHttpRequest is dependent of, but you will have to add them to your main project anyway, so it is not necessary.
  6. Try to build the project, it should do so without errors.
  7. Open your main project
  8. From finder, drag your library .proj-file into your main project (in Xcode, so that it 'lands' onto the main project file)
  9. The library project should now appear under your main project (still in XCode). It should be expandable and you should be able to see the library project files as well. If it doesn't, try closing all open projects and reopen the main project.
  10. Select the main project, and select target. Under Build Phases - Link Binary With Libraries, click the +-sign.
  11. In the list of frameworks you should see your library project (called something like libname.a). Select that file
  12. The newly added file might appear red in the list of frameworks, don't worry, it works anyway. Guess it's a bug.
  13. Still under target, go to Build Settings
  14. Under Header Search Paths add the relative search path to where the library .h-files are. This is relative to your main projects .proj-file. (For example ../some folder/libproject/)
  15. Hopefully your main project will build without errors and the library project will be built at the same time, using the same configuration as the main project.

I have no idea if this is a good approach or if there is some easier way to do it. However, I like this, since I can use the library project in several projects. And if I want to update the library project, I only have to do it in one place, and the other projects will be updated as well, since they all reference the same project.

Edit1:
I had some problems with library projects using objective c categories. I received unrecognized selector sent to instance errors in runtime when trying to call those methods. This problem was solved by following the answer given here.

  1. Go to build settings of the target in the main project and add -ObjC to the entry called Other Linker Flags

Edit2:
I found this template for creating Universal frameworks. I haven't tried it, but I guess something like this would work as well.

Xcode Project-Wide compiler flag

You can set project wide compiler flags in the project settings under the "Language" section of the "Build Settings" tab.

Sample Image

converting an non ARC project to ARC

I was trying to convert a projet to ARC and after creating a new one and including the files from the old one - one of the issues i got was

Cannot assign to 'self' outside of a method in the init family

The selector name MUST begin with init - not only that - in my case the init selector was:

-(id)initwithPage:(unsigned)pageNum {...}

Notice the small 'w'.

I have changed it to:

-(id)initWithPage:(unsigned)pageNum {...}

Notice the capital 'W'!

My problem was solved.

I hope this helps someone.

Add -fobjc-arc flag to new files created in Xcode automatically

If you want all new files to be ARC and all existing files to be MRC, it seems it would be easier to flag all existing files with -fno-objc-arc and change the target to be an ARC target. Then all new files will be ARC by default.

Errors when adding C libraries to a project with ARC

You can turn off ARC for the specific library, by adding the -fno-objc-arc compiler flag.

Go in your Target Settings > Build Phases > Compile Sources, and add the compiler flag to every implementation file from the library.

Further information can be found here: How can I disable ARC for a single file in a project?

ARC convert for before iOS 5 SDK

Convert your project to ARC using convert to arc tool of Xcode. Check below screen shot.Sample Image

XCode 4.2 opens with no toolbar, project navigator

For that sake of helping out future readers this is the clearer explanation of how to fix this issue:

XCode will open the project with the interface configured the same way as the last file that was opened. If your interface is opening with the toolbar and project navigator minimized it means that you double clicked a file and opened it in its own window. XCode thinks that this was the last file opened so it uses this as the default.

To resolve: reconfigure your window by right clicking on the top of the window and selecting "show toolbar" then open whichever panels you prefer to have open. Then make sure there are no other windows associated with that project open. Close XCode and reopen. Voila!



Related Topics



Leave a reply



Submit