Swift

The language for the rest of us

August 2014 JaxArcSig

Who am I

  • David Fekke
  • Develop on .NET, Node, Android and iOS
  • JaxNode User Group

Why another language

  • Apple needed a modern language
  • C++, C and Objective-C
  • Alternatives to Apple's tools have become popular

History of Apple modern development

  • Steve jobs gets fired
  • Starts NeXT 1985
  • Objective-C
  • Chris Lattner joins Apple in 2005

Chris Lattner

  • Doctorial work on LLVM
  • He made improvements to Obj-C
  • Added properties
  • ARC memory management

Features taken from the most popular languages of the last 10 years

  • Python
  • C#
  • Javascript
  • Scala
  • Go

Swift

  • C without the overhead of C
  • type safe
  • uses type inference
  • parens not needed for loops or conditions
  • Don't need semicolons to end statements
  • no pointers needed

Functional

  • Has lots of functional features
  • Use let to make variable a constaint
  • use of closures common
  • functions inside of functions

Hello, World

  • var myText = "Hello World!"
  • println(myText)

Type notations

  • var x = "My string" // infers x will be a string
  • var y = 5 // infers value is a number
  • var z: Int = 4
  • var a: String = "My other string"
  • var myPrimes: [Int] = [1,2,3,5,7,11]
  • var myDict:Dictionary<Int,String> = [1:"Some", 2:"Value"]

Functions

func greet(name: String, day: String) -> String {
       return "Hello \(name), today is \(day)."
}
func sayHello() {
       println("Hello there.")
}
func sumOf(numbers: Int...) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
sumOf()
sumOf(42, 597, 12)

Closures and Lamdas

var numbers = [20, 19, 7, 12]

numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result
})

let sortedNumbers1 = sorted(numbers, { 
    (r1: Int, r2: Int) -> Bool in 
    return r1 > r2 } )

let sortedNumbers2 = sorted(numbers, { (r1, r2) in return r1 > r2 } )

let sortedNumbers3 = sorted(numbers) { $0 > $1 }

Classes

struct Resolution {
    var width = 0
    var height = 0
}
class VideoMode {
    var resolution = Resolution()
    var interlaced = false
    var frameRate = 0.0
    var name: String?
}

Inits and Functions

class Vehicle {
    var make: String?
    var model: String?
    var year: Int?
    
    init(val1: String, val2: String, val3: Int) {
        make = val1
        model = val2
        year = val3
    }
    
    func whatAmI() {
        println("I am a vehicle of make \(self.make) and model
                 \(self.model) of the year \(self.year)")
    }
}
let myVehic = Vehicle(val1: "Dodge", val2: "Charger", val3: 2007)
myVehic.whatAmI()

Inheritance

class Car: Vehicle {
    var wheels:Int = 4
    
    override init(val1: String, val2: String, val3: Int)
    {
        super.init(val1: val1, val2: val2, val3: val3)
    }
}

DEMO

Other Swift Features

  • Optionals
  • Optional Chaining
  • enums syntax
  • structs syntax
  • switch statement does not require break

Questions

Contact me

  • David Fekke
  • Skype: davidfekke
  • email: david fekke at gmail dot com
  • twitter: @davidfekke