Apple(s) and Orange(s): A swift Overview of Swift

Apple(s) and Orange(s): A swift Overview of SwiftA look at Apple’s programming languageThomas MallickBlockedUnblockFollowFollowingMay 23Left: Apple’s new Swift logo | Right: Taylor Swift’s new album coverTaylor Alison Swift released her first album in 2006.

In the thirteen years since the release of her unimaginatively-named album Taylor Swift, she has rocketed to international acclaim.

Her music’s unique transition over that time period has kept fans excited, devoted, and, most of all, screaming her melodies in any place where off-pitch renditions won’t provoke the ire of others.

Apple’s Swift shares a similar rise to fame.

Apple first released Swift in 2014.

In the five years since its release, the programming language has become a superstar, ranking as the 7th most popular programming language worldwide according to the Stack Overflow 2018 Developer Survey.

Swift’s popularity surge over its relatively short lifetime boggled me and led to an investigation of how Swift came to be, what kind of language it is, and what features it offers.

To describe my curiosity using Taylor Swift’s hit Everything Has Changed:“I just want to know [Swift] better, know [Swift] better, know [Swift] better nowI just want to know [Swift] better, know [Swift] better, know [Swift] better nowI just want to know [Swift] better, know [Swift] better, know [Swift] better nowI just want to know [Swift], know [Swift], know [Swift]”So now, let me Begin Again.

Photo by Andreas Dresson UnsplashWhy Swift?Developed by Apple for four years in secrecy, Swift is Apple’s successor to Objective-C.

Until Swift’s release in 2014, millions of lines of Objective-C code powered Apple products.

That code still runs many of Apple’s product features but Objective-C is hard to read and hard to learn.

Don’t just take my word for it: Objective-C ranked as the ninth most dreaded programming language worldwide in that same Stack Overflow 2018 Developer Survey.

So, somewhere in the gilded halls of Apple headquarters, Chris Lattner (the main author of LLVM and the Clang compiler) started concocting an “update” to Objective-C, or as he describes it:“Objective-C without the C.

”This “update” would later be released as Swift and would be designed to work with the massive body of existing Objective-C code written for Apple products.

*Apple to Objective-C:*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 UnsplashThe Language“Swift takes language ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list”— Chris LattnerSwift 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 on the left 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 FeaturesType InferencePretty simple and self-explanatory.

The variable type is inferred; in this case, “variable” is inferred as a string.

Isn’t that just gr8.

var variable = "Hello, world!"Looping ExamplesI 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.

// —- LOOPING OVER A DICTIONARYlet ourClass = ["students": 9, "professors": 1]for (typeOfPerson, num) in ourClass {print(typeOfPerson, ":", num)}// —- LOOPING OVER RANGEfor index in 1.

<10{print(index)}GenericsWhile 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.

// —- NON-GENERICfunc maxMe(number: Int, otherNumber: Int) -> Int {if (number > otherNumber){return number}else{return otherNumber}}// —- GENERICfunc maxMeGeneric<T: Comparable>(number: T, otherNumber: T) -> T{if (number > otherNumber){return number}else{return otherNumber}}print("Non-Generic: ", maxMe(number: 3, otherNumber: 4))print("Generic: ", maxMeGeneric(number: 3.

017, otherNumber: 4.

10))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 FeaturesFinally, 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:// —- INSERTION SORT —–var ????.= ["????", "????", "????", "????", "????", "????", "????"]var ????.= 1, ????.= 0for ????.in ????.


count{let ????.= ????[????]var ????.= ????.while ????.> ????.&& ????[????.- ????] > ????. More details

Leave a Reply