Touch Code Magazine

Posts about Swift, Apple, iOS, conferences, and more

How to write code which is ARC, non-ARC, iOS5.0 and iOS4.x compatible

I’ve been trying to use only ARC 3rd party libraries ever since iOS5 came out and I still stand by this decision. The new features are the future, not the old features – believe me. Couple of days after I was desperate to make AFNetworking working (non-ARC btw) at all and had this unpleasant taste in my mouth after the encounter with it I stumbled upon another library which totally amazed me.

Enter MBProgressHUD.

I want to say that this is a very carefully crafted API. I liked it instantly and loved the way they put it: no matter what’s your project just include the .h and .m and it’ll work – now, ain’t that just nice?

Being compatible with everything

So why would you like to write code which is compatible with both ARC and pre-ARC? If you work on a project of yours it’s highly unlikely you’ll need that. On the other hand if you’re writing a library for others to use – that might be a good choice.

Let’s just have a look at a short piece of code, to see how it all works for MBProgressHUD:

You can see that the author cared about the quality of his code just by these few lines. Now as you see the features are as follows: objc_arc_weak means you’re on ARC and iOS5, otherwise if you at least have objc_arc then you’re on ARC and iOS4.x and finally if non of the above – it’s good old (more old the good of course) non-ARC and doesn’t matter which iOS.

In your actual code you can check only for objc_arc – that’d be when you have retain/release etc.

Well, this covers how to make your code compatible if you care for the people using your stuff … and if you need a HUD class for your app’s loader/spinner do checkout MBProgressHUD – it just works, no matter arc/pre-arc :)))

Pin it

Marin Todorov is an independent iOS consultant and publisher with complimentary background in server and desktop development. He started developing on an Apple ][ more than 20 years ago and keeps rocking till today. Meanwhile he has worked in great companies like Monster Technologies and Native Instruments, has lived in 4 different countries, and (more recently) is one the founding memebers of the raywenderlich.com tutorial team. Besides crafting code, Marin also enjoys bloging, writing books, teaching and training others, and speaking at mobile conferences. He sometimes open sources his code. He walked the way to Santiago.

Website: http://www.touch-code-magazine.com

1 Comment

  1. Pingback: DTFoundation 1.4.2 | Cocoanetics

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Back to top