"Thinking in Swift", a beginner's perspective

This year’s NSScotland kicked off with “Thinking in Swift”. A tutorial based workshop designed for those who, whilst having some experience with the Swift programming language, are otherwise writing and creating their apps with the mindset of a language they are more experienced in.

This year's workshop was presented by Daniel Steinberg who also presented last years “A Swift Kickstarter”.  As a some-what novice in Swift, previous to this year's workshop I had only a small amount of experience, I was concerned about whether Daniel’s workshop would be a little over my head but luckily this was not the case.  The style of the workshop and Daniel’s explanations made it easy to follow and ensured no one was left behind.

Daniel’s workshop was divided into 6 easy to follow tutorials. Each segment focussed on a new set of methods available in Swift that developers may not be aware of. To understand these new techniques a series of example projects were created in playgrounds, a feature new to XCode 7.  Each example had a situation that could be overcome using the new methods available in Swift.  One such example was in Daniel’s segment on Map, Filter and Reduce; 3 higher order functions that support Swift’s standard Array Library.

His example consisted of an array containing “sales” of an application in a given week.  In this tutorial each method was explained by way finding a way to apply them to the array of sales in question.

First the revenue gained from a given week's “sales” had to be displayed as an array.  To do this the map function is used.  The Map method transforms each element of an array using a given function.

 

func revenueForCopiesSold(copies: Int) -> USDollar {return round(Double(copies) * unitCost * sellersPercentage * 100) / 100}

the function applied to each day's worth of sales.

 

To demonstrate the value of the Filter function Daniel presented a scenario where only days where 4 or more units were sold in a day.  The filter function is ideal in this scenario as it will create an array of elements that pass a certain criteria (like if the units sold is greater than four).

Finally all that is needed to be done with the array is condense each day's revenue into one single value. This can be done using the Reduce function where the sum of an array can be calculated simply by using the following:

 

        myArray.reduce(0){ $0 + $1 }

 

Of all the things I took away from the “Thinking in Swift” workshop, there was one lesson that I will take from this workshop that was not expected.  This was in the value of GameKit in creating “regular” apps.

During a session on Sequences and Generators, Daniel showed how easy it was to distribute data in a random fashion through the use of GameKit’s random number facilities.  Daniel showed us the three methods of random distribution that GameKit has to offer.  Each of these distribution methods would return values within a given range however using different methods to distribute the values it could generate.

The first distribution method shown, GKRandomDistribution, was perhaps the closest to a truly random distribution of values.  With GKRandomDistribution, there is no weighting on any specific number in the range.  You could imagine this as being similar to a dice.  When a dice is rolled there is equal chance that it will land on 3 as there is a 6 and so on.

The second distribution method shown, GKGaussianDistribution, introduced a pattern to the way that numbers were distributed.  In this case values were distributed in a “curved” fashion where numbers at the “ends” of the range (that is the highest and lowest value) will be distributed less often than those at the “peak” (the middle) of the range.

The final distribution method discussed was the GKShuffledDistribution.  This method assigns random numbers in an equal fashion.  By using Daniel’s example this means it will use each day of the week the same number of times (or as close to that as it can).

 

GKRandomDistribution - [4, 8, 5, 8, 7, 9, 10]

GKGaussianDsitribution - [0, 2, 12, 20, 14, 3, 0]

GKShuffledDistribution - [8, 7, 7, 7, 7, 8, 7]

Example of how many times a random number in the range 0-6 will appear using the different types of distribution over the course of 50 iterations.

 

If you perhaps cannot find a use for this then the lesson should at least be that you shouldn’t dismiss the GameKit library completely based on its name.  There could be a lot of useful things available that could be of use to you and you don’t even know it.

I thoroughly enjoyed this year's NSScotland workshop “Thinking in Swift”.  The workshop provided me not only with useful methods and classes that I can use in developing apps but also a useful insight into the world of Swift programming and how it differs from Java, my usual language of choice.

If you’d like to find out more about Daniel and his teaching you can find him here: http://dimsumthinking.com/ or on twitter @dimsumthinking.  You can also find Daniel’s book, “A Swift Kickstart”, an introductory level book for Swift, available here.  And don’t forget to check back here for videos from the NSScotland conference and more.