You’ve heard the big news from WWDC. Apple has a new modern language and it is named Swift. Please note that it shouldn’t be mistaken with the existing swift parallel scripting language. We at Telerik were eager to try the new language and write some code and our first impressions are very positive.
In fact, if you are a registered apple developer you can start experimenting with the Swift language right now. You just have to download the Xcode 6 beta from your developer account. The language reference is also available in iTunes for free, or you can read the online reference. Another good resource can be found in iOS 8 Developer Library that is marked as pre-release.
So, let's get started.
First, create a new single view application as you did it before. Do not forget to specify Swift as your language when specifying the project name.
When the wizard finishes you will have a project with the same structure as it was written in Objective-C. The only difference is that all header files are missing and m files are replaced with swift files.
It is quite easy to use third party frameworks with Swift, just add the desired framework form the target settings. In this case we will use TelerikUI.framework. If you don't know how to add it, follow the steps described in our online documentation.
You have to do one more step in order to mix Objective-C and Swift code. You should add at least one Objective-C file. This will instruct the Xcode to create the so-called bridging header. So, open the New file dialog and choose Objective-C file. Name that file Bridge.m and save it.
You will be asked to configure a bridging header file - click Yes. An .h file will be created following the naming convention: YourProjectName-Bridging-Header. Include the following lines in that file:
This way all Telerik UI classes will be available for use in your swift files.
Now you can start writing some code. Open the ViewController.swift file and add the following lines in viewDidLoad method:
var chart =
Everything is similar to Objective-C, you can use all Cocoa Touch frameworks and APIs. The named parameters are also here, but there are fewer braces and no semicolons :)
In order to show the chart, we should add some data. The following lines will create a simple sine wave:
var y =
As you can see even constants like M_PI are available. Its interesting that you can change the (..)two dots in the for cycle with (...)three dots. This way you will include also the final value when iterating.
You should create a series in order to add this data in the chart:
var series =
var xAxis =
And the result is here:
The full sample code is available on our git repository.
You can download Telerik UI for iOS here.
So, this is it. Using Swift is really easy and intuitive.