Objective-C Tricks: Scope your Assignments

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

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.

No Comments

Brent Simmons Talks about Sync

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.

1 Comment

Apps I’m Using: 2013 In Review Edition

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.

Development

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.

 General

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.

No Comments

Blackbar for iOS

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.

No Comments

NSCache

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.

No Comments

Writing an iOS app with Corona

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.

No Comments

Portland Code School Meet & Greet

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.

No Comments

On iCloud Syncing

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.

No Comments

Seven Deadly Sins of Modern Objective-C

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.

No Comments

UIImage Category Methods

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!

1 Comment