Slide 1

Slide 1 text

Collections in Swift

Slide 2

Slide 2 text

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, ...

Slide 20

Slide 20 text

Sequences

Slide 21

Slide 21 text

SequenceType GeneratorType

Slide 22

Slide 22 text

SequenceType requirements func generate() -> Generator GeneratorType requirements func next() -> Element?

Slide 23

Slide 23 text

One element at a time

Slide 24

Slide 24 text

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) }

Slide 25

Slide 25 text

Iteration » contains(_:) » minimumElement() / maximumElement() » map(_:) / filter(_:) / reduce(_:combine:) » prefix(_:) / dropFirst(_:)

Slide 26

Slide 26 text

demo

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

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)

Slide 41

Slide 41 text

Mix-and-match » append(_:), appendContentsOf(_:) » insert(_:), insertContentsOf(_:) » removeAtIndex(_:), removeRange(_:) » removeAll()

Slide 42

Slide 42 text

demo

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

This session 1. The “Problem” 2. How Swift's collections work 3. New approaches

Slide 46

Slide 46 text

demo

Slide 47

Slide 47 text

SwiftDoc.org

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Thank you! Nate Cook @nnnnnnnn