20
A Few Interesting Things In Swift Ara Hacopian B’more on Rails 6/10/2014

A Few Interesting Things in Apple's Swift Programming Language

Embed Size (px)

DESCRIPTION

Learn about Apple's new programming language, Swift.

Citation preview

Page 1: A Few Interesting Things in Apple's Swift Programming Language

A Few Interesting Things In Swift

Ara Hacopian

B’more on Rails

6/10/2014

Page 2: A Few Interesting Things in Apple's Swift Programming Language

Ara Hacopian

http://www.smartlogic.io

http://www.twitter.com/ahacop

http://www.github.com/ahacop

SmartLogic

Page 3: A Few Interesting Things in Apple's Swift Programming Language

Some Cool Stuff● Type Inference● Explicit Mutability● Optionals● Named Parameters● Enumerations● Exhaustive Switch● Closures● Generics

Page 4: A Few Interesting Things in Apple's Swift Programming Language

Type InferenceYou can do this:

var x : Int = 0var y : String = “foobar”var z : Bool = true

Or, you can just do this:var x = 0var y = “foobar”var z = true

Page 5: A Few Interesting Things in Apple's Swift Programming Language

Type Inference

var x = 1x = “quux” ← COMPILER ERROR

Page 6: A Few Interesting Things in Apple's Swift Programming Language

Typed Collectionsvar mammals: String[] = [“dog”, “cat”]var people = [“dan”, “eric”, “tom”]

var numbers = [1, 2, 3]numbers.append(“ara”) ←COMPILER ERROR

Page 7: A Few Interesting Things in Apple's Swift Programming Language

Explicit Mutability

var x = 0x = 1x // x == 1

let y = 0y = 1 ← COMPILER ERROR

Page 8: A Few Interesting Things in Apple's Swift Programming Language

Never Be Nil

var x : Int = 0x = nil ← COMPILER ERROR

Page 9: A Few Interesting Things in Apple's Swift Programming Language

OptionalsOptionals handle the absence of a value.

Either: 1. there is some value, and it equals x

var foo : Int? = 41 // { Some 41 }

2.or, there is no value at allvar bar : Int? // nil

Page 10: A Few Interesting Things in Apple's Swift Programming Language

Optional Declaration

var intOptional : Int?var stringOptional : String?var boolOptional : Bool?

Page 11: A Few Interesting Things in Apple's Swift Programming Language

Optionals: UnwrappingYou can unwrap the value contained in

the optional by using the bang operator, like so:

var foo : Int? = 41foo! == 41 // true

But if it’s nil, you will get a runtime error, so don’t do that.

Page 12: A Few Interesting Things in Apple's Swift Programming Language

Optional Bindingvar possibleNumber = "2"if let actualNumber = possibleNumber.toInt() { println("\(possibleNumber) has an integer value of \(actualNumber)")} else { println("\(possibleNumber) could not be converted to an integer")}

Page 13: A Few Interesting Things in Apple's Swift Programming Language

Optional Chainingclass Person { var name : String var address : Address?

init(name:String, atAddress address:Address?=nil) { self.name = name self.address = address }}

class Address { func sendLetter() { println("Sent a letter!") }}

let ara = Person(name: "Ara")let paul = Person(name: "Paul", atAddress:Address())

let people = [ara, paul]

for person in people { person.address?.sendLetter()}

Page 14: A Few Interesting Things in Apple's Swift Programming Language

External Param Namesfunc join(

string s1: String,toString s2: String,withJoiner joiner: String) -> String {

return s1 + joiner + s2}

join(string: "hello", toString: "world", withJoiner: ", ")

Page 15: A Few Interesting Things in Apple's Swift Programming Language

Enumerations

enum Compass {case North, South, West, East

}

enum Status {case OnTime, Delayed(Int, String)

}

Page 16: A Few Interesting Things in Apple's Swift Programming Language

Exhaustive SwitchIn Swift, the compiler enforces that a switch statement contains a case for every possible value for that type.

enum Status {case OnTime, Delayed(Int, String)

}

let trainStatus = Status.Delayed(5, “banana on tracks”)

switch trainStatus {case .OnTime:

println(“Train is on time!”)case .Delayed(let minutes, let reason):

println(“Train delay: \(minutes) minute(s), Reason: \(reason)”)

Page 17: A Few Interesting Things in Apple's Swift Programming Language

ClosuresJust like blocks in ObjC...but there’s a lot of optional syntax.

let names = ["Paul", "Ara", "Eric", "Tom", "Dan"]func backwards(s1: String, s2: String) -> Bool { return s1 > s2}

1. sort(names, backwards)2. sort(names, { (s1: String, s2: String) -> Bool in return

s1 > s2 })3. sort(names, { s1, s2 in return s1 > s2 } )4. sort(names, { s1, s2 in s1 > s2 } )5. sort(names, { $0 > $1 } )6. sort(names, >)

Page 18: A Few Interesting Things in Apple's Swift Programming Language

Genericsstruct IntStack { var items = Int[]() mutating func push(item: Int) { items.append(item) } mutating func pop() -> Int { return items.removeLast() }}

Page 19: A Few Interesting Things in Apple's Swift Programming Language

Genericsstruct Stack<T> { var items = T[]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() }}

Page 20: A Few Interesting Things in Apple's Swift Programming Language

Questions?

http://www.smartlogic.io/

http://www.twitter.com/ahacop

http://www.github.com/ahacop