mikeash.com: just this guy, you know?

Posted at 2012-03-31 03:09 | RSS feed (Full text feed) | Blog Index
Next article: Friday Q&A 2012-04-13: Nib Memory Management
Previous article: Friday Q&A 2012-03-16: Let's Build NSMutableDictionary
Tags: arc code hack objectivec sourcecode
Introducing PLWeakCompatibility
by Mike Ash  

As a way of atoning for the lack of a Friday Q&A today, I'm pleased to introduce PLWeakCompatibility. Do you like ARC? Do you need to support older OS releases? Do you wish you could use __weak variables on those older OSes? If so, then I have good news for you! PLWeakCompatibility is the solution you've been looking for.

Using PLWeakCompatibility is easy. Add one file to your Xcode project, add two compiler flags, and you're off to the races. If you have MAZeroingWeakRef in your app already, then PLWeakCompatibility will use it to handle weak references on older OSes. If you don't, then it will use its own implementation, which is considerably simpler but somewhat slower. When your app runs on a newer OS with built in weak reference support, PLWeakCompatibility passes all calls through to the native support.

As you can probably tell from the prefix, PLWeakCompatibility comes from my employer, Plausible Labs. I had a hand in writing it, but I didn't do it alone! You can now use __weak on any OS where you can deploy ARC, so grab PLWeakCompatibility on GitHub now!

Did you enjoy this article? I'm selling whole books full of them! Volumes II and III are now out! They're available as ePub, PDF, print, and on iBooks and Kindle. Click here for more information.

Comments:

__weak on iOS 4 sounds really nice. Could there any problems with the App Store Review?
I don't anticipate any problems with review, since the symbols in question are ones the compiler inserts automatically, so they wouldn't be considered "private" API. So while I can't predict the whims of Apple's reviewers on any given day, I don't see how this code would cause any more trouble than usual.
This is awesome! Have you considered creating a Podspec for this? https://github.com/CocoaPods/CocoaPods/wiki/A-pod-specification
The procedure for adding it to your own project is so simple that I'm not sure it would be worthwhile. I wouldn't object if somebody else wanted to put one together, though!
Reflecting on whether or not this will be allowed in the app store.

If it gets rejected while it's a little bit of work to retro-fit it to use just MAZeroingWeakRef it's not more than maybe an hour or two work depending on how much you used the __weak symbol.

There are some API calls that will end up in your binary that otherwise wouldn't exist in iOS 4.0, those specifically being objc_initWeak, objc_storeWeak, objc_destroyWeak.

Only time will tell.

Comments RSS feed for this page

Add your thoughts, post a comment:

Spam and off-topic posts will be deleted without notice. Culprits may be publicly humiliated at my sole discretion.

Name:
The Answer to the Ultimate Question of Life, the Universe, and Everything?
Comment:
Formatting: <i> <b> <blockquote> <code>.
NOTE: Due to an increase in spam, URLs are forbidden! Please provide search terms or fragment your URLs so they don't look like URLs.
Hosted at DigitalOcean.