Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting with Objective-C: To ARC or not to ARC?

According to Apple's ARC Documentation, there are a fairly significant number of changes to the way that one develops software when using ARC.

As a complete beginner to Objective-C, would it be better to start off with ARC disabled, with the idea that it would give me a better low-level understanding of what is going on behind the scenes? Or has ARC essentially deprecated the 'old way' of doing things, to the point that it's not really worth spending time learning?

like image 558
Marty Avatar asked Dec 17 '11 05:12

Marty


People also ask

Does Objective-C ARC?

All Objective-C and Objective-C++ code should be compiled with ARC enabled.

How do I enable ARC in Objective-C?

ARC may be explicitly enabled with the compiler flag -fobjc-arc . It may also be explicitly disabled with the compiler flag -fno-objc-arc .

What is ARC and non ARC?

The simple answer is that in non-ARC projects you have to control almost all memory operations (ownership, release time and etc.) by yourself. On the other hand, in ARC enabled projects most work done by system.

What is Automatic Reference Counting in iOS?

Automatic Reference Counting (ARC) is to track and manage the app's memory usage . ARC automatically frees up the memory used by class instances when those instances are no longer needed.


1 Answers

This is basically an opinion question, and is therefore fairly dangerous.

My Opinion is a qualified yes. It is worth learning basic memory management. The qualification being don't get bogged down in it. Learn what ARC is doing for you under the hood with some very simple projects. Once you have a basic understanding of how to handle memory management, i.e. how to avoid retain cycles(as jemmons alluded to they can still be a problem with ARC). Once you have a basic grasp of memory management. Start using ARC.

Also as Jason Coco pointed out ARC handles memory management for (to put it simply) NSObject subclasses. So all of the CF Objects you will still be handling yourself, if you need to use them.

An excellent explanation about what ARC is doing for you under the hood can be found in the WWDC2011 Session 323 - Introducing Automatic Reference Counting.

But there are some other considerations that might steer your decision.

What devices do you need to target?

If you plan to target iOS 4.3 and up ARC effectively handles memory management for you.(of NSObject subclasses)

If you plan to target iOS 4.2 then you will not be able to use weak references(you will use unsafe_unretained). iPhone 3g? & iPod touch 2nd gen are stuck at this OS level, because there are many of these devices still in service many developers are still targeting them.

If you plan to target iOSs earlier than 4.2(This would be rare) you will definitely need to learn MRC(Manual Reference Counting).

If you plan to build Mac Apps, there is a garbage collector available on that platform. ARC is also an option(full ARC 10.7, no weak support 10.6).

like image 109
NJones Avatar answered Sep 18 '22 18:09

NJones