Slug jacob schwartz cover?fm=jpg&fl=progressive&q=75&w=300

F-Yeah Swift: 4 Features That Changed How I Code

Swift’s unique and forward-thinking combination of features fosters more productive and readable code. Highlighting his favorite language features like Swift’s type system and immutability, Jacob Schwartz discusses how Swift contributed to his app’s codebase compared to other languages. Learn what aspects of Swift make it easier and more fun to use than other languages.

I use Swift to build an app for a startup I work for, Glint, and would like to highlight the top four features of the language that had the biggest effect on my work & code compared to Objective-C or C++.

Strong Type System (1:37)

The great type system helps reduce the cognitive load when reading code. It helps code feel more explicit. For instance, this Objective-C dictionary could contain any type of keys and values, or it could even be nil:

Get more development news like this

@property (strong, nonatomic) NSMutableDictionary *selectedFilters;

In Swift, however, the definition immediately conveys that it’s a dictionary only of strings to arrays of strings:

var selectedFilters = [String: [String]]()

I use the type system to control logic flow throughout the app, not having to check for classes of objects.

Immutability (1:51)

In C++, these are all equivalent definitions for an immutable ivar, with more and more const correctness. This is a huge mess and feels like a hack.

class Puter {
  void compute(obj *ptr);
  void compute(const Obj *ptr);
  void compute(Obj * const ptr);
  void compute(const Obj * const ptr);
  void compute(const Obj * const ptr) const;

In Objective-C, it does not even feel like a hack — it is a hack; you need to manage mutable vs. immutable versions of strings, arrays, etc.

With Swift, these declarations take very little effort — it simply takes choosing between var or let in the variable or property declaration.

var dateString = formatter.stringFromDate(date)
// vs
let dateString = formatter.stringFromDate(date)

Properties (3:29)

Although the code is messy, it properly demonstrates the power and ease of properties in Swift.

var responseRate: Double? {
  didSet {
    let percent = min(100, max(0, Int((responseRate ?? 0)* 100)))
    let baseString = NSLocalizedString("$0 ResponseRate", comment: "PulseTabResponseRate")
    let percentString = "\(percent)%"
    middleLabel.text = baseString.stringByReplacingOccurrencesOfString("$0", withString: percentString)

Initially when exploring the Swift docs, I thought it was just syntactic sugar for properties. However, when writing programs, it turns out that it makes architecting your code more powerful and simple. This comes from a view controller, where I have a setter that takes a double as the response rate for some survey. Then I format the string that the user sees when the double is set. This is how I easily did data binding on this particular view controller.

Awesome Community (4:25)

There are fantastic resources for learning and helping fix problems online, as well as Swift meetups and informative newsletters that help foster the learning environment. It makes writing code easier and more fun.

Although there are still some problems with it, such as inability to support old iOS versions with new Swift versions, tool instability, and random linker errors, it’s constantly being improved upon with new releases. Working with these Swift features and more, I am able to write dense and expressive code that is actually fun to write, making me more productive compared to other languages.

About the content

This content has been published here with the express permission of the author.

Jacob Schwartz

Jacob Schwartz is a Senior Engineer working in Swift at Glint. In his free time he’s an active hobbyist game developer and has shipped several iOS games including iso and Ragdoll Boxing. His other loves include beer, bicycling, and design. He’s a secret Canadian hiding amongst the Americans, but don’t tell anyone!

4 design patterns for a RESTless mobile integration »