Swift - Ogólne informacje
Kompilowany
statycznie typowany
obiektowy (opcjonalnie)
Pierwsza wersja - 2014
Od 2015 - open source (Apache License 2.0)
Obecna wersja 2.2 / Preview 3.0
Kompilacja, uruchamianie programów
REPL - swift
Kompilacja - swiftc program.swift
"Tryb interpretowany" - #!/usr/bin/swift
Data types
Int / UInt
Float
Double
Bool
String
Character
Optional
Built-in data structures
Arrays
Dictionaries
tuples
sets
// Arrays
var 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"
// Tuples
for (key, value) in dict {
print(key)
print(value)
}
func test() -> (Int, String) {
return (42, "bazinga")
}
var (a, b) = test()
// Sets
var 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Loops
// for
for var i=0 ; i < 10 ; i++ {
print(i)
}
for var i in 0...10 {
print(i)
}
// for-in
let EnterpriseCaptains = ["Jonathan Archer", "James T. Kirk", "Jean-Luc Picard"]
for captain in EnterpriseCaptains {
print(captain)
}
//while
var n = 2
while n < 100 {
n = n*2
}
var m = 2
repeat {
m = m*2
} while m < 100
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Functions
func greet(name: String) -> String {
return "Hello, (name)"
}
//variable number of arguments
func printout(values: Int...) {
for var val in values {
print(val)
}
}
//nested function
func add8(value: Int) -> Int {
var y = 8
func add() {
y += value
}
add()
return y
}
//closures and anonymous functions
var numbers =[4, 8, 15, 16, 23, 42]
numbers.map({number in 2*number}) // lambda number: 2*number
//simpler
numbers.map({2*$0})
//even simpler
numbers.map {2*$0}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Swift - Krzysztof Skarupa

  • 1.
    Swift - Ogólneinformacje Kompilowany statycznie typowany obiektowy (opcjonalnie) Pierwsza wersja - 2014 Od 2015 - open source (Apache License 2.0) Obecna wersja 2.2 / Preview 3.0
  • 2.
    Kompilacja, uruchamianie programów REPL- swift Kompilacja - swiftc program.swift "Tryb interpretowany" - #!/usr/bin/swift
  • 3.
    Data types Int /UInt Float Double Bool String Character Optional
  • 4.
    Built-in data structures Arrays Dictionaries tuples sets //Arrays var 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" // Tuples for (key, value) in dict { print(key) print(value) } func test() -> (Int, String) { return (42, "bazinga") } var (a, b) = test() // Sets var 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) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  • 5.
    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") } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
  • 6.
    Loops // for for vari=0 ; i < 10 ; i++ { print(i) } for var i in 0...10 { print(i) } // for-in let EnterpriseCaptains = ["Jonathan Archer", "James T. Kirk", "Jean-Luc Picard"] for captain in EnterpriseCaptains { print(captain) } //while var n = 2 while n < 100 { n = n*2 } var m = 2 repeat { m = m*2 } while m < 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  • 7.
    Functions func greet(name: String)-> String { return "Hello, (name)" } //variable number of arguments func printout(values: Int...) { for var val in values { print(val) } } //nested function func add8(value: Int) -> Int { var y = 8 func add() { y += value } add() return y } //closures and anonymous functions var numbers =[4, 8, 15, 16, 23, 42] numbers.map({number in 2*number}) // lambda number: 2*number //simpler numbers.map({2*$0}) //even simpler numbers.map {2*$0} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  • 8.
    Classes and Objects classPet { 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 } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  • 9.
    Structures struct Ship { varshipClass: 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 } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  • 10.
    Protocols protocol ExampleProtocol { varmember: 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 } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18