Swift: Impressions!

Swift Logo

In this story, I will share my impressions, what I liked and what I didn't liked about Apple's newest programming language.

I was enrolled in a Mobile Application Development module last semester, taught with Swift. I have mixed emotions but the overall impression about the language is very good.

Swift was introduced by Apple in 2014, and already in its third release. This sounds exciting, and someone would argue that this means the language is under active development which is great. I agree, but this makes tutorials, resources and lecturers teaching the language very hard. You want to find a resource about CoreData? They are in Swift 2 and sometimes is quite hard to convert it to Swift 3. Not to mention the life of lecturers and online course instructors where their courses become irrelevant and they need to update their resources. Is good to see changes of course, but why not well thought from the beginning? The changes made are not only in terms of performance but also in terms of the language syntax, that’s what hurts more.

Here is a quote I grabbed from the Internet that described the language:

Swift is a new programming language for iOS and macOS apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun. Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to reimagine how software development works.

Swift has some very cool features including Higher order functions (although called closures), inferred data types, string templating, pattern matching and more.

Roughly speaking, in my opinion, Swift is very close to Python and Haskell because it borrows some very powerful features from multiple languages including C#, Python, Ruby and Functional Programming languages like Haskell.

Labelled parameters

Although this is the consensuses of Objective-C, Swift (3.0>) allows the programmer to label the parameters of functions, tuples etc.

Here is an example:

func convertTemperature(fromFahrenheit value: Double) -> Double {
  return (value - 32) * .5556
}

func convertTemperature(fromCelcious value: Double) -> Double {
  return value * 1.8 + 32
}

As you can see the two functions above reads very nicely. Here how they look in the main script:

  let fahrenheit = convertTemperature(fromCelcious: 30.0)
  let celcious = convertTemperature(fromFahrenheit: 50.0)

Not only the code looks more readable and clean, but also allows method overloading in a very nice way.

Closures

Closures is another cool feature, which in reality is what we call Higher-Order functions in other languages.

quakeFeed.quakes = self.quakes.sorted(by: { (q1: Double, q2: Double) -> Bool in
    return q1.magnitude > q2.magnitude
})

Does this looks cool? Stay with me, we can make it shorter. We can eliminate the code to a single line and still achieve the same result:

quakeFeed.quakes = self.quakes.sorted(by: { (q1: Double, q2: Double) -> Bool in return q1.magnitude > q2.magnitude})

Still not there. Because of Swift’s powerful inferring type from context, we can omit the type of the parameters instead of being explicit:

quakeFeed.quakes = self.quakes.sorted(by: { q1, q2 in return q1.magnitude > q2.magnitude})

Swift can infer the type of the q1 and q2 parameters as well as the return type since the return statement is a boolean condition.

Still not there. We can also omit the return keyword, since single-expression closures can implicitly return the result:

quakeFeed.quakes = self.quakes.sorted(by: { q1, q2 in q1.magnitude > q2.magnitude})

Still not there. We are almost done:

Swift also provide a shorthand for parameters to in-line closures. This can be done by using $0, $1, $2 and so on, to refer to the closure’s first argument, second argument and third argument respectively. Therefore we have something likes this:

quakeFeed.quakes = self.quakes.sorted(by: {$0.magnitude > $1.magnitude})

The { $0.magnitude > $1.magnitude } is the closure that nicely states if the first parameter’s magnitude is greater than the second parameter’s magnitude.

Optionals

Optionals is also a great feature. Is quite hard to get used to it, but Xcode does a great job to remind you about it. Optional is a variable that can either have a value or no value, in which case is nil.

This can be useful when you are dealing with variables that can have no value during the app execution or if a function (search function for example) can return no result. So a function searches for a string in a text and can either return the location of the string in the text or nil if the string is not found.

Constants

I used constant variables in Swift more than I did in all other languages I have used in the past combined! That’s because mainly Xcode will suggest with a warning to declare a variable or a class attribute as a constant if it hadn’t changed since it has been declared. Which is great.

The whole ecosystem

Xcode Playgrounds

It was very nice that the whole development was available through Xcode IDE. Now, this has its pros and cons. I liked that the Xcode comes with git client, powerful debugging tools and unit testing inspector. The best though is that it comes with iOS simulator where you can run your apps and test them without the need to connect a real iOS device. Not only this, but you can also have UI testing built-in running on the simulator!

On the other hand, depending on a single tool (IDE) has its disadvantages. Xcode is not perfect and is not that much configurable, is not open source and the software releases with new features is very slow in comparison with other IDEs and text editors. Is not that you can’t do iOS/macOS development without Xcode, as far as I am aware there are alternative options but for sure Xcode is the best option out there as of now.

Bringing everything together

With all that said, I think Swift is a great programming language and if you are interested in future iOS and/or macOS development you should definitely learn Swift over Objective-C.

The modern syntax and features make the language even a great first programming language. Xcode ships with a Playground feature where you can test - visually - Swift code which makes the experience of learning your first programming language even better.

Xcode Playgrounds

References

© 2019 Rafael Papallas
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.