Objective-C Tricks: Scope your Assignments

March 14, 2014

There’s a new Objective-C pattern I’ve been experimenting with lately, which looks like this:

- (void)viewDidLoad;
    [super viewDidLoad];
    self.shareButton = ({

		CGFloat xPos = self.view.width - 80.0f;
        CGFloat yPos = self.view.bottom - 44.0f;
        CGRect frame = CGRectMake( xPos, yPos, 70.0f, 44.0f );
        UIButton *button = [[UIButton alloc] initWithFrame:frame];
    	UIImage *normal = [UIImage imageNamed:@"share.png"];
        UIImage *disabled = [UIImage imageNamed:@"share-disabled.png"];
        [button setImage:normal forState:UIControlStateNormal];
        [button setImage:disabled forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];

This syntax probably isn’t familiar to Objective-C programmers, but it’s actually simple. It’s a trick that takes advantage of a compiler extention where putting a variable by itself as the last line within () braces acts as “returning” that variable, meaning you can use the whole expression as an assignment.

What’s the point? Chances are you’ve worked on an iOS app or two where some big controller class had a viewDidLoad method that’s grown out of control. The kind of class that’s managing a dozen views, each with their own content, setup, positioning and so on. In these classes the viewDidLoad method can be hundreds of lines long, impossible to quickly read and parse, and require refactoring for even small changes.

The advantage of this syntax is that it wraps each assignment into a neat little bundle, no matter how much extra work is related to it. Temporary variables, subviews, configuration and whatnot all live within the scope’s braces, so it’s easy to see at a glance what code belongs to each specific object. You can give temporary variables nice names too, since they all live within their own scope. Ever have a method where you have to name things like passwordFieldFrame, confirmPasswordFieldFrame, …? Now each of those CGRects can just have the nice, short name “frame” without worrying about stepping on the toes of another object.

There’s an argument to be made against this syntax, and I’m not completely sold on the idea. In an ideal world you’d want to split these view controllers into more managable subcontrollers, instead of trying to pretty up the mess. It’s also a little hacky, relying on a language feature that’s not intended for this purpose. But despite these problems I still kind of like what it does. It’s hard to say no to anything that makes code more manageable and easy to read.

Brent Simmons Talks about Sync

March 04, 2014

If you don’t follow Brent Simmons’ blog, you’re missing an excellent series of posts about Core Data and syncing as he writes the sync engine for Vesper. If you’ve ever wondered exactly why syncing is a hard problem to solve, even for experienced developers, this is a must read.

Check out the entire series here.

Apps I’m Using: 2013 In Review Edition

December 14, 2013

I’ve always enjoyed reading about other people’s setups, especially when I discover some small utility or tool that I didn’t know existed. After a fresh install of Mavericks a few months ago, I figured this is a good time to write up my own list.


AppViz - App Store sales reports. I recently started using version 3, which syncs to a backend service for data collection. There are a few web-only competitors I’ve tried over the past few years, but AppViz is just nicer.

Dash - Xcode documentation viewer. I’ve only used this for a few weeks, but I’ve been impressed. I’ve used similar tools before and always ended up back in Xcode, but it looks like Dash is going to stick around.

Deploymate - Ensures your iOS or OSX project is compatible with older operating systems. I’ve used this to find a couple crashing bugs in Showyou. Like many development tools, this one pays for itself after the first use.

Gitbox - Still my favorite visual git client. I haven’t seen many updates for it this year, but it still works well.

Kaleidoscope 2 - The nicest file comparison app you’ll find, and it integrates perfectly with Gitbox. I use the two on a daily basis.

TextMate 2 - I’ve heard TextMate isn’t the cool kid’s editor anymore, but I’m still using it. I probably wouldn’t have even switched from 1.5 to 2.0 if it wasn’t for better compatibility with Mavericks.

Tokens - A very nice app that handles sending out App Store promo codes. I don’t use it often, but I’m glad I have it every time I do.

xScope - Still a must-have utility for doing UI work.


Dropbox - Dropbox is still a must-have for me. A year or two ago I moved everything from ~/Documents into it. Makes doing a clean OS install much easier.

1Password - I started using 1Password during the version 4 betas, switching over from Wallet.app (which is no long being developed). Using separate, randomly generated passwords is critical considering how often I see website security breaches in the news. 1Password works much better than Wallet, and better still than Apple’s new iCloud Keychain.

Things - Still my favorite task management app. Though there are lots of iPhone competitors I’d like to try, having a desktop app is critical for me.

Aperture - I don’t dislike Aperture, but whenever I use it I feel like there should be something better. If so, I haven’t found it yet.

Cloud - Screenshot and file sharing. Unfortunately it’s a bit buggy on Mavericks, and I’ll probably end up switching to Dropbox for sharing at some point.

Ember - This is where I’ve been putting random images and screenshots. Everything that doesn’t go in Aperture, goes here. Though I gave up on its predecessor (Little Snapper) last year, Ember won me over again.

Melo - It’s a simple app that connects iTunes to Last.fm. Last.fm’s recommendation engine is still my go-to place to find new music.

Simplenote - The new Simplenote app is not quite as nice as Notational Velocity, but I’m giving it time to grow on me.

Tweetbot - The best Twitter client for OSX.

Soulver - A scratchpad for calculations. More useful than a simple calculator, easier to use than a spreadsheet.

VLC - Still the best general purpose, do-it-all video player.

Blackbar for iOS

August 26, 2013

Blackbar is one of those rare games that takes a simple idea and nails the execution. It’s a word game that’s delightfully simple, with an engrossing story behind it. And made in Portland. Get it here.


March 26, 2013

If you’re the type of developer who’s always digging into the Cocoa frameworks to find the stuff that makes your life easier, you’re probably using NSCache. It’s a great class after all. Just store your temporary objects in an NSCache instead of an NSMutableDictionary, and you won’t have to worry about memory usage. Right?

Not exactly. A recent conversation on Twitter reminded me of my own experiences working with NSCache and memory warnings. Although it’s natural to expect NSCache to clear itself in response to a memory warning notification, this isn’t what actually happens. NSCache does automatically evict objects, but this behavior is very unclear and undocumented. In short, don’t depend on it. It’s possible for your app to receive memory warnings, and even crash due to low memory, all without NSCache lifting a finger to help. Instead, keep doing what you’ve always done. Register for low memory notifications and manually release any objects that aren’t essential, whether they’re stored within NSCache or otherwise.

There’s nothing wrong with using NSCache, but until the docs say otherwise don’t count on it to do the work for you.

Writing an iOS app with Corona

March 05, 2013

From The Beast Within:

The experience was overall a good one, with expected frustrations that come with writing code. I still label myself as a “hobbyist” developer, because there’s still tons I don’t know about Corona and app development. But if there’s one thing that can be said about Corona, it’s that it enables almost anyone to write an iOS app; if you can pass an intro-to-Computer Science class, you can write an iOS app with Corona.

As a long-time iOS developer I don’t often think about mobile apps outside of Objective-C. I still believe native frameworks are the correct answer for most apps, but it’s good for everyone that even a non-programmer can bring something to market in his spare time.

Portland Code School Meet & Greet

February 18, 2013

Portland Code School is hosting a meet and greet on March 6th for prospective students. This will be their third session, and I really recommend checking it out if you or anyone you know is interested in taking on web development as a career:

Applications are now being accepted for the Portland Code School Summer Session! Join us for an informal gathering to meet current students and find out more about the program over beer and snacks. We'll start the evening with a presentation about the format of the program, what you'll learn, how we'll help you find a job at the end, and the application process and requirements. If you've dabbled with Rails but haven't quite made it to paid developer status yet this may be just the program for you.

Take a look at the event page for full details.

On iCloud Syncing

February 07, 2013

If you follow the Cocoa development community you probably know how big of an issue iCloud syncing is. Watching the WWDC videos from last year you might think iCloud is the next big thing in Cocoa, much like Bindings or Core Data. It’s only when you start really looking at it in depth that you realize just how many bugs and edge cases you have to account for. Trying to enable Core Data iCloud syncing is a huge undertaking for advanced developers, and even the more reliable document and key value store syncing can have its share of problems.

More and more developers have started down the iCloud path and realized too late what the actual issues are. Some have updated their apps to remove iCloud support, and others are not even shipping until Apple fixes some of the problems they’ve run into. I’m definitely part of this crowd with some of my apps I had hoped to ship last year. I started with Core Data before I decided it wasn’t worth the risk and moved my app to document based data storage. Even now I’m not sure I’m on the right track though. My app has a very simple data model, but even after putting in a good amount of work and re-writing things two or three times there’s still work to do.

There are other solutions, including open-source sync frameworks like TICoreDataSync, and Dropbox’s new Sync API. Some of these look quite promising, but in the end it’s impossible for any third party library to achieve the same simplicity and support iCloud promises. I’m hoping Apple realizes just how many developers are affected and is working to improve things. It’s clear that this is hurting the Mac OS and iOS software ecosystem, and if even if some of the problems are resolved it will be hard getting developers to trust iCloud when there are so many horror stories out there.

Seven Deadly Sins of Modern Objective-C

February 06, 2013

I've had this post cooking for a long time, and I think it's ready to unveil. If you code Objective-C, this is going to offend you and that's good. If you aren't offended, then you don't care, and that's bad. This list isn't about stylistic things like which line new braces go on (new ones, duh). This list is about potential problems with the code you're writing on an objective scale. So let's get started.

Read more on Ash Furrow’s blog. I admit, I’m guilty of a few of these. Hopefully number seven, not using automated testing, will soon be crossed off my list. I’ve experimented with it in the past, but still haven’t adopted it on any of my shipping Cocoa projects.

UIImage Category Methods

March 30, 2012

If you’ve Googled about resizing or cropping UIImage objects, chances are you’ve read Trevor Harmon’s blog post on the subject. Trevor’s solution is great, but unfortunately a little dated since changes to the iOS SDK have introduced a few bugs and compiler warnings if you try to use the original code written in 2009.

I’ve been maintaining my own copy for the past year or so, and I’ve finally gotten around to open sourcing it on GitHub. This fork includes a few of my own changes and bug fixes described in the comments of the original post. If you need to resize or crop an image check it out!

Introducing the ZeroNinetyNine.com bundle!

February 23, 2012

I’m happy to announce that WeatherMin is now available for $0.99 through the ZeroNinetyNine.com bundle, along with eleven other great Mac apps. This is a special one-day sale, including these awesome Mac apps:

  • SpriteRight
  • Yummy FTP
  • Protect Files
  • iSleep
  • Cockpit
  • Scrawl
  • iDatabase
  • Records
  • Compress Files
  • Deskscribble
  • All-in Yoga

Check it out here!

Tutorial: Disable the iPhone 4’s Front Camera

August 24, 2011

I’ve been negligent in posting new articles this year, but despite appearances this blog isn’t quite dead yet. I’ll begin regular postings again soon, but in the meantime here’s a link to an article I guest authored over at iCodeBlog:

One option is to replace the standard camera controls with a custom interface, but that’s a whole lot of work if you just want to prevent the user from taking a photo with the front camera. Fortunately there’s another option: put a transparent button over the “switch camera” button, which will intercept touch events and show an alert dialog. It sounds simple, but as you’ll see there are a few tricks to actually getting this to work.

Check out the rest here!