3

I am using some non-ARC code in my ARC project, namely Three20. I have added all the appropriate compiler flags and all works well. However, I need to subclass some of the Three20 classes, and I'm not sure if I should add the compiler flag to my new file for non-ARC, or if the compiler will figure it out, and add the appropriate release calls.

Just to recap: - ARC project in XCode 4 - Includes non-ARC code (Three20) - Need to subclass something defined in non-ARC files - Do I need to add release calls? - Do I need to add compiler flag for non-ARC in subclass?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571

2 Answers2

8

Michael,

ARC is a compile time technology that determines retain/release semantics based upon whether a given slot in memory will persist beyond the current method/function invocation. Because of this, you can safely intermix subclasses using ARC or not. I do it all of the time. I also do it in categories. Unlike garbage collection, ARC is quite focussed on just the storage space in each method/function. BTW, most of iOS's frameworks do not appear to yet use ARC. Hence, any subclass you make of a framework class has this "problem" and it just isn't an issue.

To answer your specific question:

Just to recap: - ARC project in XCode 4 - Includes non-ARC code (Three20) - Need to subclass something defined in non-ARC files - Do I need to add release calls? - Do I need to add compiler flag for non-ARC in subclass?

Your subclass of a non-ARC superclass can be either ARC or not. As the default setting for your app is ARC, you need do nothing to your subclass.

Andrew

adonoho
  • 4,339
  • 1
  • 18
  • 22
2

Yes, you need to add non-ARC flag (-fno-objc-arc) and retain / release calls.

Don't add it if your subclass does use ARC, but I recommend against this, because it would just be asking for trouble.

Guillaume
  • 21,685
  • 6
  • 63
  • 95
  • 1
    Why would it be asking for trouble? Any time someone makes an ARC-enabled `UIViewController` subclass, they're doing this. ARC is done on a per-file basis, not a per-class or per-class-hierarchy basis. As such, there is *no* problem at all with making an ARC subclass of a non-ARC base class. – BJ Homer Jan 10 '12 at 20:07