August 04, 2015
There are a ton of jokes about that 2 hardest things in programming. Some say it’s naming things, estimating and off-by-one errors ;) Some say it’s estimating & getting paid. I think it’s nailing down your requirements (so you know what needs to get done) and keeping your code at a single level of abstraction.
What do I mean by a single level of abstraction? Well, consider some old crufty Objective-C code:
NSArray *myGists = [[NSArray alloc] initWithObjects: [NSString stringWithString:@"text of gist 1"], [NSString stringWithString:@"text of gist 2"], nil]; // do stuff with myGists [myArray release];
That code does something with an array of Gists. But instead of being able to just think about gists, the programmer working on this code also has to think about managing the memory that holds those gists (in
release). So they’ve got to keep 2 different levels of abstraction in their mind. Those objects aren’t just gists to them, they’re also hunks of memory.
Sure, under it all the gists are really hunks of memory. And there’s got to be code somewhere that’s aware of that. But it doesn’t have to be in the same place as gist-y operations like starring a gist or editing the text. And that’s how it is with code to hook in to web services:
- Somewhere your code has to know about the low level network stuff
- Somewhere it has to handle JSON
- Somewhere it has to work with gists (or whatever your model objects are)
Those three levels of abstraction don’t need to (and shouldn’t) overlap. It’s much less exhausting to work on code at a single level without having to keep flipping your understanding of the code up & down to higher and lower abstractions.
You don’t need to use libraries like SwiftyJSON and Alamofire. But they’re often a really good wrapper around lower levels of abstraction. And if they’re open source so you can tweak the code later if you need to, what have you got to lose?
This tutorial is an excerpt from my iOS Apps with REST APIs guide. It’ll teach you how to build Swift apps that get their data from web services without hundreds of pages about flatmap and Core Whatever. iOS Apps with REST APIs guide is the book I wish I had on my first iOS contract when I was struggling to figure out how to get the API data showing up in the app I was building.Start Building iOS Apps with REST APIs now for $29
(Buying for a group? Grab the discounted Team License from LeanPub.)