Swift is Apple’s new programming language, which promises to allow you to write iOS and OS X apps more easily than with Objective-C. Apple have released a free eBook titled “The Swift Programming Language” which provides an overview of the language. This is where I gained my main impressions on Swift. To give you a little background on myself: while I have had an interest in getting into app development for a while, at this point I haven’t touched any Objective-C – I am currently programming mainly with C, C++ and Python.
The first thing I thought when I started looking at the Swift code examples, is how much it reminds me of Python. Consider the first Python script most of us learned:
print("Hello, world!")
Now consider Swift’s syntax:
println("Hello, world")
At the same time, the method for assigning variables reminds me very much of the syntax of Javascript. To assign a variable in Swift:
var mynumber = 23
However, defining constants seems a little weird to me, as you have to use the keyword “let” rather than “var”:
let pi = 3.14
Swift will automatically work out that the constant “pi” is a float, although it does give you ways to explicitly define the type, especially if you want the precision of a double.
Next, moving onto arrays, the resemblance to Python is uncanny, to take the example that Apple have used in their book:
var shoppingList = ["catfish", "water", "tulips", "blue paint"] shoppingList[1] = "bottle of water"
Control flow and loops employ a very C-like syntax, although the braces again make the code appear similar to JavaScript. To take another code example from the book:
let individualScores = [75, 43, 103, 87, 12] var teamScore = 0 for score in individualScores { if score > 50 { teamScore += 3 } else { teamScore += 1 } }
I’ve only scratched the surface of Swift in this short post, and if you’re interested in app development I’d recommend downloading the free eBook and having a browse through. I was rather put off getting into App development by how busy I was and by the syntax of Objective-C. I really like how Scratch looks and think when Xcode 6 is released I might finally be motived to build a native iOS app.