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 a whole book full of them. It's available for iBooks and Kindle, plus a direct download in PDF and ePub format. It's also available in paper for the old-fashioned. Click here for more information.

Comments:

Felix Schulze at 2012-04-01 15:57:11:
__weak on iOS 4 sounds really nice. Could there any problems with the App Store Review?

mikeash at 2012-04-03 00:31:43:
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.

Ben Scheirman at 2012-04-06 15:19:54:
This is awesome! Have you considered creating a Podspec for this? https://github.com/CocoaPods/CocoaPods/wiki/A-pod-specification

mikeash at 2012-04-07 18:08:57:
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!

Robert Payne at 2012-05-13 04:22:27:
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:
Web site:
Comment:
Formatting: <i> <b> <blockquote> <code>. URLs are automatically hyperlinked.
Hosted at DigitalOcean.