Apple recently made some press with their release of the Swift language. That release provides me an opportunity to do a couple of things:
- Catch up on what’s going on with the latest Xcode
- Play with and learn a new language
My first language, roughly a millennia ago, was BASIC and if I sat down to list the number of languages I’ve learned since then, I suspect it’d be between 10 and 20, especially if I count scripting languages like bash/csh and my love/hate relationship with Perl.
At that time, I was self-taught and I can assure you my code demonstrated that. I pored over examples from the few magazines that published code or books that had examples I could type in to figure out how they did what they did.
I had the occasion to look back at a program I wrote which was published in a book of games (you had to type in the games line-by-line with a checksum at the end to determine if you typed it in accurately) and the code might charitably be described as ‘spaghetti’. More accurately, it was clearly the code of someone with a great deal more enthusiasm than knowledge.
In any case, in that BASIC from days of yore, I might have typed the following:
110 LET D=8
120 LET E=12
130 LET RESULT = D + E
140 PRINT "RESULT =";RESULT
I was tickled, to say the least, to see the return of the ‘LET’ keyword in Swift, not only for the reminder of code gone by, but to see that, as with many things, what was once old can be new again!
In Swift, I might now say something like:
1 let apples = 3
2 let oranges = 5
3 let fruitTotal = apples + oranges
4 println("I have \(fruitTotal) pieces of fruit.”)
Not that different, No?
However, not all is as it once was. Where I was limited to upper case variable names, we can now use a variety of unicode. Not entirely sure that in all cases it’s a good idea, but in Swift we can do that following:
let π = 3.14159
let 你好 = "你好世界"
(Excerpt From: Apple Inc. “The Swift Programming Language.”)
Yes, we can now name our variables after sensible things like Pi, but we can also name them using (the aged Programmer in me shudders slightly) emoji.
For those of us with a C or a C++ background, we can continue to use either /* style comments */ or // Comments.
Because Swift, where possible, is trying to make things easier, both generally and for beginners, we can also leave out declaring data types in many cases.
Swift is smart enough to infer the type based on the value, for example:
let intNumber = 3
let floatNumber = 3.14159
This versus the more explicit approach of C:
int intNumber = 3;
float floatNumber = 3.14159;
Note Given the amount of time I’ve written C code, my fingers REALLY want to keep hitting a semi-colon at the end of the declarations (or any line!), as in the C example above, but in SWIFT, they’ve gone away. I shouldn’t miss them, but clearly my reflexes, at least, do!
For all that Swift felt like a pleasant melange of languages still banging around in my head, there are definitely some new concepts which I still have to wrap my head around.
One of these is the Optional, a notion which doesn’t exist in C or Objective-C.
Basically, and without duplicating the language document, an optional says that a given variable may take on values of a given type, say an Integer or they may take on a nil value indicating that they have no value – not just a zero or something like that, no value at all. This is still a bit of a head scratcher, but if we look at something like the following it becomes a bit more clear:
1 var serverResponseCode: Int? = 404
2 // serverResponseCode contains an actual Int value of 404
3 serverResponseCode = nil
4 // serverResponseCode now contains no value
(Excerpt From: Apple Inc. “The Swift Programming Language.”)
In this example we see that the serverResponseCode can not only hold an Integer value, but it can also, validly, simply be a nil. The question mark (‘?’) allows the programmer to make this explicit.
Related to this, the exclamation point (‘!’) has been overloaded in a similar context to say “I know the optional referenced definitely has a value, so use that”. It looks like this:
1 if convertedNumber != nil
2 println("convertedNumber has an integer value of \(convertedNumber!).")
3 }”
(Excerpt From: Apple Inc. “The Swift Programming Language.”)
Another thing that appears to have gone missing entirely with Swift (and likely not missed by many) is the absence of the ‘&’ or the “address of” operator.
Additionally, one of the challenges with C or Objective-C (or C++) was memory management which, though it improved over time, could still be difficult to manage.
With Swift, Apple has now implemented what they call Automatic Reference Counting (ARC) which should make it more difficult to leak memory (though I’m sure not impossible for the dedicated!) and easier to manage memory in general.
One of the other features of Swift, or really an extension built in to their IDE, Xcode, are playgrounds.
Playgrounds allow developers to develop code “live” on a running system and see the effects of changes immediately. Effectively, it’s an excellent way to short circuit the compile/link/run loop of the past in favor of making changes and seeing the results immediately.
Below I’ve linked to the Balloons playground which is a fun way to play with graphics and the Sprite library an see the effects immediately. Using playgrounds, you can also build in documentation and live tutorials and play with them in real time. This strikes me as a very powerful tool for teaching new programmers.
With the release of Swift, Apple is trying to accomplish several things:
- Provide cross platform support for application development for both iOS (iPhones and iPads) and OS X (Mac apps)
- Provide a powerful language that allows the advanced developers to create applications
- Provide a language designed to be easy to use for beginners
- Provide a language that allows application to run faster – Apple claims more than 2x faster than Objective-C and more than 8x faster than Python (obviously for some applications)
Any one of these are probably not enough to justify Yet Another Language, but taken in concert, it’s a pretty powerful combination.
I’ve been working through some tutorials for Swift, playing with Playgrounds and knocking some of the rust off the programming parts of my brain and it’s been a great deal of fun.
I haven’t even touched on some of the major advancements that have come with some of the latest iterations of iOS like the SpriteKit and built-in physics engines.
One of my favorite ways to learn, and this has been true since I was a kid, is to either modify an existing program or write something with a graphics element – okay, just call it a game – by way of making learning more fun and I can say I have had fun learning Swift and playing. And, really, at the end of the day, that’s why I started programming in the first place – because it was fun. It’s great to feel that again!
Links:
- https://developer.apple.com/swift/ – Swift Intro
- https://itunes.apple.com/us/book-series/swift-programming-series/id888896989?mt=11 – Swift programming Series (iBooks)
- https://developer.apple.com/swift/blog/downloads/Balloons.zip – Balloons.playground (www.apple.com)
- https://twitter.com/swiftlangnews – Swift on Twitter
- http://www.raywenderlich.com/ – Tutorial Resources (www.raywenderlich.com)
0 Comments