Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

My First Hello World with Apple's Swift and Telerik UI for iOS

DZone's Guide to

My First Hello World with Apple's Swift and Telerik UI for iOS

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

 Originally written by Nikolay Diyanov

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. But let's keep things simple, open the New file dialog and choose Objective-C file. Name that file Bridge.m and save it.

Include only the following line in that file:

#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:NSMutableArray = []
for x in 0..10 {
var y = 100.0 * sin(M_PI * CDouble(x) + 100.0)
  items.addObject(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.

You can download Telerik UI for iOS here.

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

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

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Published at DZone with permission of Doug Winfield, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}