let ages = [13.3, 17.5, 18.9, 21.2]
let firstPlusOne = ages.first + 1
Slide 7
Slide 7 text
let ages = [13.3, 17.5, 18.9, 21.2]
let firstPlusOne = ages.first + 1
Value of optional type ‘Int?’ not unwrapped; did you mean to use ‘!’ or ‘?’
Slide 8
Slide 8 text
let ages = [13.3, 17.5, 18.9, 21.2]
let firstPlusOne = ages.first! + 1
// firstPlusOne = 14.3
Slide 9
Slide 9 text
let ages: [Double] = []
let firstPlusOne = ages.first! + 1
Slide 10
Slide 10 text
let ages: [Double] = []
let firstPlusOne = ages.first! + 1
! error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=
Slide 11
Slide 11 text
let ages = [13.3, 17.5, 18.9, 21.2]
let average = ages.reduce(0, +) /
Double(ages.count)
// average = 17.725
Slide 12
Slide 12 text
let ages: [Double] = []
let average = ages.reduce(0, +) /
Double(ages.count)
Slide 13
Slide 13 text
let ages: [Double] = []
let average = ages.reduce(0, +) / 0
Slide 14
Slide 14 text
let ages: [Double] = []
let average = ages.reduce(0, +) / 0
! error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=
ed: This doesn’t actually crash, but would for [Int]
Slide 15
Slide 15 text
let ages = [13.3, 17.5, 18.9, 21.2]
let last = ages[4]
Slide 16
Slide 16 text
let ages = [13.3, 17.5, 18.9, 21.2]
let last = ages[4] // off by one
Slide 17
Slide 17 text
let ages = [13.3, 17.5, 18.9, 21.2]
let last = ages[4] // off by one
error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=
!
Slide 18
Slide 18 text
“Safe”
Slide 19
Slide 19 text
let ages = [13.3, 17.5, 18.9, 21.2]
let last = ages[4] // off by one
Slide 20
Slide 20 text
let ages = [13.3, 17.5, 18.9, 21.2]
let last = ages[4] // off by one
// sure, fine, whatever
Slide 21
Slide 21 text
let ages = [13.3, 17.5, 18.9, 21.2]
let last = ages[4] // off by one
// last == ???
let limit = 100
var foundCount = 0 as CFIndex
var documentIDs = Array(repeating: 0 as SKDocumentID,
count: limit)
_ = SKSearchFindMatches(CFIndex(limit),
&documentIDs, &foundCount)
for i in 0..
Slide 46
Slide 46 text
let limit = 100
var foundCount = 0 as CFIndex
var documentIDs = Array(repeating: 0 as SKDocumentID,
count: limit)
_ = SKSearchFindMatches(CFIndex(limit),
&documentIDs, &foundCount)
for i in 0..
Slide 47
Slide 47 text
let limit = 100
var foundCount = 0 as CFIndex
var documentIDs: [SKDocumentID] = []
documentIDs.reserveCapacity(limit)
_ = SKSearchFindMatches(CFIndex(limit),
&documentIDs, &foundCount)
for i in 0..
Slide 48
Slide 48 text
let limit = 100
var foundCount = 0 as CFIndex
var documentIDs = Array(repeating: 0 as SKDocumentID,
count: limit)
_ = SKSearchFindMatches(CFIndex(limit),
&documentIDs, &foundCount)
for i in 0..
Slide 49
Slide 49 text
let limit = 100
var foundCount = 0 as CFIndex
let documentIDs = UnsafeMutablePointer
.allocate(capacity: limit)
defer { documentIDs.deallocate(capacity: limit) }
_ = SKSearchFindMatches(CFIndex(limit),
documentIDs, &foundCount)
for i in 0..
Slide 50
Slide 50 text
let limit = 100
var foundCount = 0 as CFIndex
let documentIDs = UnsafeMutablePointer
.allocate(capacity: limit)
defer { documentIDs.deallocate(capacity: limit) }
_ = SKSearchFindMatches(CFIndex(limit),
documentIDs, &foundCount)
for i in 0..
Slide 51
Slide 51 text
func bubbleSort(_ array: inout [T]) {
guard !array.isEmpty else { return }
for n in 1.. array[i] {
swap(&array[i - 1], &array[i])
}
}
}
}
Slide 52
Slide 52 text
func bubbleSort(_ array: inout [T]) {
guard !array.isEmpty else { return }
array.withUnsafeMutableBufferPointer { buffer in
for n in 1.. buffer[i] {
swap(&buffer[i - 1], &buffer[i])
}
}
}
}
}
Slide 53
Slide 53 text
Really Not
Safe at All
Slide 54
Slide 54 text
var age = 2000
let agePointer = UnsafeMutablePointer(&age)
agePointer.pointee = 10
// age == 10
Slide 55
Slide 55 text
var age = 2000
let agePointer =
withUnsafeMutablePointer(to: &age) { p in
return p
}
agePointer.pointee = 10
// age == 10
Slide 56
Slide 56 text
var age = 2000
let agePointer = UnsafeMutablePointer(&age)
agePointer.pointee = 10
// age == 10
undefined
behavior