Apple(s) and Orange(s): A swift Overview of Swift
By: Thomas J. Mallick
Tags: Programming Languages, Coding, Programming, Swift Programming, Swift

Swift was designed for use with iOS, macOS, watchOS, tvOS, z/OS, and I’m sure whatever future “__OS” Apple decides to wow the world with next year.

But wait a nanosecond.

it seems like a little much for Apple to develop Swift all because Objective-C is hard to read and hard to learn, right? I mean, Apple’s programmers are paid big bucks to know Objective-C backward and forward.

But when you look at it, you have to think of app developers as well. It seems like Apple wanted to lower the barrier of entry for app developers which would allow more apps to be created for iOS instead of Android.

In 2014 (the year Swift was released) the App Store generated $4.5 billion of revenue for the company. In 2018, it generated $11.4 billion of revenue.

I’m not going to say that Swift was responsible for all that jump in revenue, but hey, I’ll say it probably helped.

By creating a readable, easy-to-understand language complete with all sorts of syntactic sugar, average people — you, me, Karen from down the street — could develop apps for Apple and help pay for the energy bill down in Cupertino.

Photo by Lee Campbellon Unsplash

The Language

“Swift takes language ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list” — Chris Lattner

Swift tries to be a “best of all world’s programming language.” As such, it supports multiple programming language paradigms.

Because it’s based on Objective-C, it inherits OOP patterns. At the same time, it offers features of functional programming languages like first-class functions and touts a new paradigm called “protocol-oriented programming” (POP).

The video is Apple’s introduction of protocol-oriented-programming at their 2015 WWDC. If you have the time, give it a watch and hopefully by the end you’ll have a good understanding of POP.

If you’re pressed for time, here’s a short tutorial that does a great job explaining.

Swift is statically and strongly typed(if you’re confused what that actually means, go ahead and click those links!).

Apple’s language-baby provides Objective-C components in a “safer” way, making it easier to find and deal with bugs. Accordingly, it doesn’t expose pointers or other unsafe accessors.

Language Features

Type Inference

Pretty simple and self-explanatory. The variable type is inferred; in this case, “variable” is inferred as a string. Isn’t that just gr8.

Looping Examples

I thought Swift made looping over a dictionary simple and easy to understand. Here you can see the syntactic sugar that Apple was going for. To me, the syntax reads like a cross between Java and Python. The curly braces feel reminiscent of Java and the ease of understanding reminds me of Python.

Generics

While not unique to Swift, the language supports generic functions. Generic functions allow functions (like maxMe below) to take in various variable types rather than being restricted to a single variable type.

Above, the function labeled “NON-GENERIC” is restricted to working with Ints while the function labeled “GENERIC” can work with doubles, floats, Ints, etc.

This helps make functions reusable and, consequently, more powerful.

Hidden Features

Finally, and obviously the most useful feature that Swift (in XCode) supports is Emoji programming. If you ever want to make programming even more challenging than it already is or hide the inner workings of your programs from viewers, I implore you to take up Emoji programming.

Not only will it bring cheer to all those who read your code after you, but it’s a great mental exercise to keep track of which emoji contains which variable.

Here’s an Emoji insertion sort for ya: