Upload
smartlogic
View
2.589
Download
3
Embed Size (px)
DESCRIPTION
Learn about Apple's new programming language, Swift.
Citation preview
A Few Interesting Things In Swift
Ara Hacopian
B’more on Rails
6/10/2014
Ara Hacopian
http://www.smartlogic.io
http://www.twitter.com/ahacop
http://www.github.com/ahacop
SmartLogic
Some Cool Stuff● Type Inference● Explicit Mutability● Optionals● Named Parameters● Enumerations● Exhaustive Switch● Closures● Generics
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
Type Inference
var x = 1x = “quux” ← COMPILER ERROR
Typed Collectionsvar mammals: String[] = [“dog”, “cat”]var people = [“dan”, “eric”, “tom”]
var numbers = [1, 2, 3]numbers.append(“ara”) ←COMPILER ERROR
Explicit Mutability
var x = 0x = 1x // x == 1
let y = 0y = 1 ← COMPILER ERROR
Never Be Nil
var x : Int = 0x = nil ← COMPILER ERROR
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
Optional Declaration
var intOptional : Int?var stringOptional : String?var boolOptional : Bool?
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.
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")}
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()}
External Param Namesfunc join(
string s1: String,toString s2: String,withJoiner joiner: String) -> String {
return s1 + joiner + s2}
join(string: "hello", toString: "world", withJoiner: ", ")
Enumerations
enum Compass {case North, South, West, East
}
enum Status {case OnTime, Delayed(Int, String)
}
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)”)
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, >)
Genericsstruct IntStack { var items = Int[]() mutating func push(item: Int) { items.append(item) } mutating func pop() -> Int { return items.removeLast() }}
Genericsstruct Stack<T> { var items = T[]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() }}
Questions?
http://www.smartlogic.io/
http://www.twitter.com/ahacop
http://www.github.com/ahacop