Telerik blogs

 


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:

#import <UIKit/UIKit.h>
#import
<TelerikUI/TelerikUI.h>

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 = TKChart(frame: CGRectInset(self.view.bounds, 30, 30))
self.view.addSubview(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 items: [TKChartDataPoint] = []
for x in 0..<10 {
var y = 100.0 * sin(M_PI * CDouble(x) + 100.0)
    items.append(TKChartDataPoint(x:CDouble(x + x * 3), y: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 = TKChartSplineSeries(items:items)
series.yAxis = TKChartNumericAxis(minimum:-100, andMaximum:100)
 
var xAxis = TKChartNumericAxis(minimum:0, andMaximum:20)
xAxis.majorTickInterval = 5
series.xAxis = xAxis
chart.addSeries(series)


And the result is here:

 

 

The full sample code is available on our git repository.
Y
ou can download Telerik UI for iOS here.

So, this is it. Using Swift is really easy and intuitive.

http://www.telerik.com/ios-ui


iOS, UI
About the Author

Nikolay Diyanov

Diyanov is the Product Manager of the Native Mobile UI division at Progress. Delivering outstanding solutions that make developers' lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.

Find him on Twitter @n_diyanov or on LinkedIn.

Comments

Comments are disabled in preview mode.