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:
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.