Apple Swift: A Look Inside a New Language
First Reaction To Swift
During last week’s keynote speech at WWDC 2014, Apple announced “an innovative new programming language for Cocoa and Cocoa Touch” called Swift. Many of us in the office initially were stunned — we even hypothesized about Apple’s motivations on our blog.
After four years of learning about Objective-C’s syntax, memory management, and design patterns, I initially found myself fighting off dread at the prospect of learning another programming language.
As the WWDC presentation continued, Swift’s benefits become more clear:
- It was going to help new and existing developers write better code.
- It had automatic memory management and easy to understand syntax.
- It was built to be fast, including the best features from Objective-C without the baggage of C.
- The sandbox would make it easy to eliminate common coding errors
The demonstration of Playgrounds started to get me excited. The presenter revealed a blimp flying across a game scene. He then made a few code changes and in the same window, you could instantly see the blimp gaining and losing altitude instead of just gliding in a straight line. Now I could write code and watch it execute immediately, or over time using the timeline assistant, seeing how my code progresses.
I was ready to start.
Getting Under the Hood with Swift Playgrounds
Once Xcode was done downloading, I started to experiment and see what Swift could do. I immediately launched the new Playgrounds tool because I am a developer — and we love to play with new tools. After writing a single line of code, I saw the code execute immediately in another panel. I didn’t have to start an iPhone simulator or output to a console window. I didn’t have to compile or wait for it to build.
Unlike other scripting languages, Playgrounds tells me right away if I entered in something wrong. Very nice.
The image above contains a small set of code I wrote, and illustrates the power of Playgrounds. The area on the left is where a developer can write Swift code with automatic error checking like Xcode. The middle section, called the Results Sidebar, shows the output of the code or the number of times an operation is executed. This feature can definitely help developers with algorithm performance and testing. The last section, called the Timeline, displays the values logged for the line of code. If the values are numeric, the timeline will show the data in a graph with time vs value. In the Timeline above, the first graph shows an array of sorted numbers and their value at a given time. The second graph shows the same array of numbers, but the value is doubled.
I did find a few problems — but that might be from me not fully understanding its capabilities. For one, Playgrounds stopped working when I tried to use other frameworks. It does work differently when the Playgrounds file is within an regular Xcode project, which I found that frustrating at times.
After fiddling with Playgrounds, I decided to experiment with Swift files and integrating with Objective-C code. I created a simple iPhone application using the Swift language and just like with any other template, Xcode created a default project with everything that I needed. I added some frameworks and I created some classes. I found that the integration was easy and straightforward. The Swift syntax took a little getting use to, but when I started to think like a Java/C# programmer again, the code started to flow very quickly. I loved it.
Overall First Impression of Apple Swift
Swift is still in beta and Apple promises to fix bugs and add new enhancements. I am going to continue working with Swift and Playgrounds, learning more about both and seeing what else I can do with it. Apple has again created something that will help developers to create beautiful, fast, and innovative mobile applications while helping reduce common coding mistakes and bugs.