Nate’s favorite part of Swift:
The Standard Library
Collections System
Slide 3
Slide 3 text
Protocol-oriented
programming
Slide 4
Slide 4 text
Protocols
in Objective-C
Slide 5
Slide 5 text
Protocols
in Swift
Slide 6
Slide 6 text
Protocols
of Swift collections
Slide 7
Slide 7 text
This session
1. The “Problem”
2. How Swift's collections work
3. New approaches
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
! " #
Slide 10
Slide 10 text
No content
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
! " #
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
No content
Slide 15
Slide 15 text
No content
Slide 16
Slide 16 text
This session
1. The “Problem”
2. How Swift's collections work
3. New approaches
Slide 17
Slide 17 text
No content
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
A sequence is a series of values
we can access one at a time
» 6'2", 5'7", 5'9", 5'4", 5'11"
» 0xEF 0xBB 0xBF 0x4E 0x61 0x74 0x65
» 22, 27, 4, 8, 28, 14, ...
» 0, 1, 2, 3, 4, 5, 6, 7, ...
let numbers = [1, 2, 3, 4]
for num in numbers {
print(num)
}
...is equivalent to:
var gen = numbers.generate()
while let num = gen.next() {
print(num)
}
Collections provide indexed
subscript access to their elements
» Arrays
» String views (i.e., "Hi there".characters)
» Sets
» Dictionaries
Slide 30
Slide 30 text
CollectionType
Slide 31
Slide 31 text
CollectionType requirements
var startIndex: Index
var endIndex: Index
subscript(i: Index) -> Generator.Element
Slide 32
Slide 32 text
Quick diversion: Index types
» Forward index types
» Bidirectional index types
» Random-access index types
Slide 33
Slide 33 text
CollectionType requirements
var startIndex: Index
var endIndex: Index
subscript(i: Index) -> Generator.Element
Slide 34
Slide 34 text
Indexed access to elements
» Collections are sequences, too
» prefixUpTo(_:), prefixThrough(_:), suffixFrom(_:)
» subscript(Range)
» count, isEmpty
» indices
Slide 35
Slide 35 text
Sequenception
Slide 36
Slide 36 text
demo
Slide 37
Slide 37 text
No content
Slide 38
Slide 38 text
No content
Slide 39
Slide 39 text
Range-replaceable collections
support arbitrary subrange
replacement
» Pretty much just arrays
Slide 40
Slide 40 text
RangeReplaceableCollectionType
requirements
init()
func replaceRange
(subRange: Range,
with newElements: C)