Avoiding Double Stuffed View Controllers in MVC

Imagine you’re building a house. After months of waiting you sit down with the architect to see the plans. Unfortunately there’s a problem. Everything you asked for is there but it’s all in one giant room! “I can’t take a poo in front of my spouse. It’s a line we don’t cross.” – you remark. Just as designing a building requires dedicated rooms for dedicated  purposes so does code. Model-View-Controller is a way to architect code into 3 distinct layers that achieve 3 separate purposes. Along with compartmentalizing code MVC also lays out a structure for how these layers should communicate with each other. The nature of Apple’s UIKit framework makes it easy to separate objects in the view layer from the controller layer. However, because it’s not strictly enforced, there’s a tendency to put app logic and data in the vcs (view controllers), leaving them looking “double stuffed”.

The Strong, the Weak and the Unowned

It’s the Fall of 2011, Adele’s “Rolling in the Deep” is blasting from little white headphones everywhere. It was a simpler time, except in the area of iOS memory management. Something was about to change. With iOS5 came the wonder of Automatic Reference Counting (ARC). App memory management became a lot easier. However, there are still a few scenarios which, if left unchecked, could have you “Rolling in the S***”.

Getters and Setters Described Using Letters

Values are associated with classes, structs and enums using properties. Properties can come in two forms, the simplest being a stored property. Thankfully Swift makes life a little simpler than in the Objective-C world by doing away with instance variables. In Swift the functionality of Objective-C iVars and properties are brought together into a single property declaration.  This comes as either a constant (let) or variable (var) and can be assigned a default value where they’re declared, or thair value can be set or modified at initialization.

Optionals Toolbox Essentials

Optionals are the keystone in the Swift Cathedral of Safety. They protect against the possibility of a value being absent, the program looking for that value and then crashing. Welcome to a guide through 6 useful tools used to navigate the landscape of question marks, exclamations marks (and even double question marks??)! When a value is optional it means that it can be in one of two states: 1) it contains something or 2) it contains nil. In fact optionals in Swift are enums with two cases: .None, or .Some<t>. Optionals are a distinct type and the compiler wont let them be used until the value has been “unwrapped”.

Get Your JSON On

JSON is a standard of data interchange that can be very handy for getting information into and out of an app. By Parsing in JSON from the outside world an app can dynamically display new information such as the latest weather, sports scores or travel information. There are many exciting API JSON feeds out there that make it possible to build an app capable of accessing information in real time over a network connect.

Diving into Swift Extensions

Extensions in Swift can provide an elegant and organized way to add functionality to structs, enums, classes and protocols. They can add intent and readability to the code, paving the way for glowing Swift poetry, choirs of singing angels and world peace*. A great way to get rolling with extensions is to see how they can be used to enhance an existing struct in the Swift standard library. This post will cover how to add simple computed properties and create an extension to group measurement unit conversions in an app. *best case scenario

Structs, What Are They Good For?

There are four scrumptious flavors of named types in Swift: protocols, enumerations, structures and classes – all custom data types that can be created and given a name. Of these, enums, classes and structs can be used to model data, protocols being used to tell the others how to behave. The choice between using a struct and a class has some interesting nuances. The hope of this post is to clear up these murky waters leaving us in a golden shower of data model clarity!

I just wanna tell you how I'm feeling

Rick Astley

An Introductory Romp in The Garden of Enumerations

A little known fact* is that the inspiration behind the 1987 chart topper “Never Gonna Give You Up” came in the form of assembly code delivered to Rick Astley while in a feverish dream state. The prevailing message of this lucid epiphany came as an enumeration:  *possibly made up enum ThingsRickAstleyWontDo { case GiveYouUp case LetYouDown case RunAround case DesertYou } // Implementation // assign the type ThingsRickAstleyWontDo to the variable var heIsNeverGoingTo = ThingsRickAstleyWontDo.GiveYouUp heIsNeverGoingTo = .LetYouDown heIsNeverGoingTo = .RunAround heIsNeverGoingTo = .DesertYou Out of the box Swift comes with a collection of built in data types for your declarative pleasure; Int, String, Double, Array etc… but Swift also allows you to define your own custom data types.