swift - krzysztof skarupa
TRANSCRIPT
Swift - Ogólne informacje
Kompilowanystatycznie typowanyobiektowy (opcjonalnie)Pierwsza wersja - 2014Od 2015 - open source (Apache License 2.0)Obecna wersja 2.2 / Preview 3.0
Kompilacja, uruchamianie programów
REPL - swiftKompilacja - swiftc program.swift"Tryb interpretowany" - #!/usr/bin/swift
Data types
Int / UIntFloatDoubleBoolStringCharacterOptional
Built-in data structures
ArraysDictionariestuplessets
// Arraysvar array: Array<Int> = [2]var array: [Int] = [2]var array = [4]
array.append(5)print(array[0])
//Dictionaries
var dict: Dictionary<String, String> = ["key": "value"]var dict: [String: String] = [:]var dict = ["key": "value", "another key": "another value"]print(dict["key"])dict["one more key"] = "one more value"
// Tuplesfor (key, value) in dict { print(key) print(value)}func test() -> (Int, String) { return (42, "bazinga")}var (a, b) = test()
// Setsvar testSet: Set<String> = ["whateva", "hamburger"]var testSet: Set = ["test", "another test"]
testSet.insert("one more test")var anotherSet: Set = ["whateva", "blah"]
testSet.union(anotherSet)testSet.intersect(anotherSet)
123456789
101112131415161718192021222324252627282930313233343536373839
Conditionals
var condition: Bool = true
if condition { print("condition met")} else { print("condition not met")}
if petType == "Dog" { print("Buy Pedigree")} else if petType == "Cat" { print("Buy Whiskas")} else if petType == "Human" { print("Buy coffee")} else { print("Relax")}
switch petType {case "Dog": print("Buy Pedigree")case "Cat": print("Buy Whiskas")case "Human": print("Buy coffee")default: print("Relax")}
123456789
101112131415161718192021222324252627282930
Loops
// forfor var i=0 ; i < 10 ; i++ { print(i)}
for var i in 0...10 { print(i)}
// for-inlet EnterpriseCaptains = ["Jonathan Archer", "James T. Kirk", "Jean-Luc Picard"]for captain in EnterpriseCaptains { print(captain)}
//whilevar n = 2while n < 100 { n = n*2}
var m = 2repeat { m = m*2} while m < 100
123456789
10111213141516171819202122232425
Functions
func greet(name: String) -> String { return "Hello, \(name)"}
//variable number of argumentsfunc printout(values: Int...) { for var val in values { print(val) }}
//nested functionfunc add8(value: Int) -> Int { var y = 8 func add() { y += value } add() return y}
//closures and anonymous functionsvar numbers =[4, 8, 15, 16, 23, 42]
numbers.map({number in 2*number}) // lambda number: 2*number//simplernumbers.map({2*$0})//even simplernumbers.map {2*$0}
123456789
101112131415161718192021222324252627282930313233
Classes and Objects
class Pet { var name: String init(name: String) { self.name = name } func sleep() -> String{ return "ZZZ" }}
class Dog: Pet { var breed: String var weightKg: Int init(name: String, breed: String, weightKg: Int){ self.breed = breed self.weightKg = weightKg super.init(name: name) } override func sleep() -> String { return "ZZZZZZ" } func speak() -> String { return "Woof" } var weightGrams: Int { get { return weightKg * 1000 } set { weightKg = newValue / 1000 } }}
123456789
10111213141516171819202122232425262728293031323334353637383940
Structures
struct Ship { var shipClass: String var name: String var cargo: Int = 0 mutating func addCargo(){ self.cargo += 1 } mutating func removeCargo(){ if self.cargo < 1 { print("Ship empty") } else { self.cargo -= 1 } }}
123456789
1011121314151617
Protocols
protocol ExampleProtocol { var member: Int mutating func process()}
class ExampleClass: ExampleProtocol { var member = 2 func process() { member += 25 }}
struct ExampleStruct: ExampleProtocol { var member = 5 mutating func process() { member *= 3 }}
123456789
101112131415161718