This is the first of a series of blogs about using Swift, Apple's new programming language that was annouced at WWDC 2014. They are not in any particular order but are about new and interesting features and differences with Objective-C. I don't intend to explain syntax or specific frameworks as there are plenty of resources available covering those already.

A completely new feature of Swift and Xcode is Playgrounds. This makes writing Swift code very interactive, typing a line of code and seeing results immediately. I will walk through two simple examples of using this in practice.

An aspect of Playgrounds is they can visually represent numeric values without writing any visual code so we'll start with a simple example using a sine wave.

To start a Playground run Xcode and select 'Get started with a playground'.

The initial Playground has a single string variable which we won't use in our example so we'll delete that and write a simple loop to calculate a sine wave five times which actually isn't that useful but will show the idea. When code is written two icons appear in the right hand column, an eye and a circle.

The eye will give the numerical value if the code is non visual or the actual view if the code generates a view. The circle will open up a new pane on the right which is called the timeline and will show a visualisation of the computed value, in this case a sine wave repeated five times.

The second example is a little more useful. This will create a subclass of UIView which will be the background for our Hackaball app which consists of a blue gradient with a faint grid of line and points. We will use Core Graphics to render the desired view. Our view class is then instantiated at a certain size.

These are a few observations from the following code which is available on GitHub:

  • In order to render the view we must override the UIView method drawRect and in Swift you have to use the keyword 'override' when overriding a superclass method. This reduces the chance of misnaming the method which would be missed in Objective-C.
  • Swift uses type inference which means that often you won't have to specify the type of a variable, for example when declaring a number, if you have '1' it will infer it is an integer, if the number is '1.0' then the inference is it's a float. However Core Graphics has it's own float type CGFloat so we have to specify the variables used to be of type CGFloat.
  • In most programming languages there is a modulo operator, for example in C it is the '%' character. Swift uses the same character for it's remainder operator which differs from modulo by allowing the use of floats and can be a negative value.
  • In Objective-C when using Core Graphics you have to manage the memory release calls, in this example that would be for 'colorSpace' and 'gradient'. Swift manages those release calls.

With the complete code we can now see how the view appears, either by looking in the timeline.

Or as here, its actual view size.

Useful links

Julian James

Julian James

Julian has been developing iPhone apps since the iPhone SDK was released, with his first app, a game, Zone Warrior going live in October 2008. He now has 10 apps on the app store with several being designed specifically for the iPad. He's been a Mac OS developer on and off for over 20 years. Other computing experience includes C, C++ and Java development in various industries, including banking, simulation and multimedia.

@jgsjames

Continue reading

Supercharge your Google Form with Google Apps Script!

I love Google Docs, and with just a little bit of JavaScript, you can supercharge any of your Google Docs using Google Apps Script. In this blog, I'll sho...

Ken Haggerty   ·   2 April 2015

Bzzzt! Opening the door using WebSockets and a Raspberry Pi

A few of us Many have been playing around with hardware here over the past year or two. Being a purely software orientated person myself I found hardware ...

Alex Barlow   ·   1 April 2015

The problem with RFPs

Innovation is hard. Doing things that haven’t been done before, by collaborating with people you’ve never worked with, to navigate obstacles that no one c...

Andy Whitlock   ·   31 March 2015