24 Days of Hello World Swift
Today is the day I introduce what I believe will be the youngest language in this series. The new language from Apple, called Swift. This is a compiled language, created for iOS, OS X, watchOS and tvOS development, designed to work with Cocoa/Cocoa Touch frameworks and the body of existing Objective-C code. Compared to Objective-C it is safer and more concise.
Core concepts supported is notably dynamic dispatch, late binding, extensible programming and similar features. These are concepts that made Objective-C flexible, but Swift is designed to address problems with performance and safety trade-offs. This includes addressing null pointer problems and optimization that can flatten out method calls. It also introduced an extensibility system, which apple refer to as “protocol-oriented programming”
History
In 2010, Chris Lattner from Apple, started the development of Swift. Several languages inspired Swift, such as Haskell, C#, Ruby, Objective-C and Python. At the Worldwide Developers Conference, in 2014, a beta version was released to Apple developers, with the full release of version 1.0 later the same year. At the same conference a free manual, called “The Swift Programming Language”, was also released.
At the same conference, this year Swift 2 was released. Later, with version 2.2 Swift was made open source, available for Apple’s platforms and Linux.
Typical uses
Swift is primarily created to support development for iOS, OS X, watchOS and tvOS, all from Apple. Whatever idea you may have for any of these platforms, you can create it with Swift.
Example Hello World
This will be a fairly short example. For the example you will need a Mac, of some sort, running OS X. I am running this on a Mac Mini (late 2012), with the latest version of OS X, El Capitan. You will also need XCode, which includes everything you need to develop for iOS, OS X, watchOS and tvOS. You can find it on Apple Store, and you can read more about it on Apples website.
With XCode installed, it’s time to start it. With the entry screen open, select “Get started with a playground”. This will take you to a page for creating a new playground. I called mine “HelloWorld”, and chose iOS as the platform. You will also need to select where to create the playground.
With the text editor open, replace everything in it with the following:
print(“Hello, World!”)
On the right hand side, you should see the all-familiar “Hello, World!n” printed (the last characters, n, is just the characters for newline). And that’s about it for the example. Now you can go on to play around with Swift, and you can find more information at Apple.
For my own use, I will not learn Swift. I use Xamarin with C# and .NET for mobile app development. That is also not an area I will focus a lot on over the next few years, as I have other things planned. More on that later.
I hope you visit back tomorrow for the next post!