After playing with Swift a bit here are my first impressions of the language. There is definitely a lot to like about it! There’s also a few things that annoy me. 😎 But first I’ll start out with a giving you a 1,000 foot overview of the language itself.
Syntax aside, Swift and Objective-C 2.0 (as opposed to the old Objective-C that Apple acquired when they bought NeXT) have a LOT in common. For starters they are both the brain child of Christ Lattner and as such both are built on top of the fabulous LLVM platform which is used for Swift’s REPL mode (called Playgrounds) and for both Swift and Objective-C’s native code generation.
Syntax
First off, I am incredibly amazed at the similarities in the syntax between Swift and Kotlin. One would almost wonder if one of them borrowed heavily from the other. In fact there are more than one comparison of the two out on the Internet. Even more of a surprise is that Google has chosen Kotlin as its official development language for the Android platform. So now iOS (actually all of the Apple operating systems) and Android each have an official language that are damn near identical to each other. One would almost be forgiven for thinking there might be a conspiracy afoot.
The Good, the Bad, and the Ugly…So far
Swift is definitely designed to protect coders from themselves. There are things missing from Swift that are present in other languages. For example, in a lot of languages that trace their lineage back to C (Java, C++, and Objective-C, to name a few) expressions return a value. This allows you to compound statements together to reduce the number of lines of code.
int c; if((c = foo()) > 0) { // Do something here if c is greater than 0. }
In the example above the expression “c = foo()
” returns the result stored in the variable ‘c
‘ which then can be used in a comparison “> 0
“. This sort of thing is not allowed in Swift because it can lead to confusing code that causes serious bugs.
Some things, such as NULL
values are still allowed but are carefully guarded so that you have to jump through a few extra hoops. In Swift you have to use NULL
values (called ‘nil
‘) indirectly.
var foo: String? = nil print("The value of foo is \(foo!)")
The question mark in the declaration above denotes that the variable ‘foo
‘ can hold a nil
value. In order to use the value you have to use the exclamation point. These serve as visual “queues” or “reminders” to help you remember that it’s possible for that variable to NOT be initialized to a proper value. In reality all variables in Swift have to be initialized. Nils are implemented using an enumeration type Optional<Wrapped>
with two values: none
and some(Wrapped)
. But as I recognize that I’m butchering all this for you, just remember that the ultimate reference is the documentation at swift.org.
In my next post I’ll go more into depth about the syntax and available resources that I’ve found. I’ll also go over integrating Swift and C/C++/Objective-C code. (HINT: It really couldn’t be easer!)