Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Collections in Swift
Search
Nate Cook
March 31, 2016
Programming
0
41
Collections in Swift
Talk at CocoaConf Chicago, March 2016
Nate Cook
March 31, 2016
Tweet
Share
More Decks by Nate Cook
See All by Nate Cook
Swift's Pointy Bits
natecook1000
0
510
Flexible Code for Generic Collections
natecook1000
8
1.2k
SwiftDoc.org Preview
natecook1000
0
280
Other Decks in Programming
See All in Programming
250830 IaCの選定~AWS SAMのLambdaをECSに乗り換えたときの備忘録~
east_takumi
0
350
私の後悔をAWS DMSで解決した話
hiramax
4
180
MCPで実現するAIエージェント駆動のNext.jsアプリデバッグ手法
nyatinte
7
1k
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
1
200
速いWebフレームワークを作る
yusukebe
3
1.5k
Zendeskのチケットを Amazon Bedrockで 解析した
ryokosuge
3
240
MLH State of the League: 2026 Season
theycallmeswift
0
210
Improving my own Ruby thereafter
sisshiki1969
1
140
複雑なドメインに挑む.pdf
yukisakai1225
4
890
ソフトウェアテスト徹底指南書の紹介
goyoki
1
130
🔨 小さなビルドシステムを作る
momeemt
3
630
rage against annotate_predecessor
junk0612
0
150
Featured
See All Featured
Designing for humans not robots
tammielis
253
25k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
We Have a Design System, Now What?
morganepeng
53
7.8k
Visualization
eitanlees
147
16k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
The Cult of Friendly URLs
andyhume
79
6.6k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
830
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
Being A Developer After 40
akosma
90
590k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
510
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.4k
Transcript
Collections in Swift
Nate’s favorite part of Swift: The Standard Library Collections System
Protocol-oriented programming
Protocols in Objective-C
Protocols in Swift
Protocols of Swift collections
This session 1. The “Problem” 2. How Swift's collections work
3. New approaches
None
! " #
None
None
! " #
None
None
None
This session 1. The “Problem” 2. How Swift's collections work
3. New approaches
None
None
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, ...
Sequences
SequenceType GeneratorType
SequenceType requirements func generate() -> Generator GeneratorType requirements func next()
-> Element?
One element at a time
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) }
Iteration » contains(_:) » minimumElement() / maximumElement() » map(_:) /
filter(_:) / reduce(_:combine:) » prefix(_:) / dropFirst(_:)
demo
None
None
Collections provide indexed subscript access to their elements » Arrays
» String views (i.e., "Hi there".characters) » Sets » Dictionaries
CollectionType
CollectionType requirements var startIndex: Index var endIndex: Index subscript(i: Index)
-> Generator.Element
Quick diversion: Index types » Forward index types » Bidirectional
index types » Random-access index types
CollectionType requirements var startIndex: Index var endIndex: Index subscript(i: Index)
-> Generator.Element
Indexed access to elements » Collections are sequences, too »
prefixUpTo(_:), prefixThrough(_:), suffixFrom(_:) » subscript(Range<Index>) » count, isEmpty » indices
Sequenception
demo
None
None
Range-replaceable collections support arbitrary subrange replacement » Pretty much just
arrays
RangeReplaceableCollectionType requirements init() func replaceRange<C: CollectionType where C.Generator.Element == Generator.Element>
(subRange: Range<Index>, with newElements: C)
Mix-and-match » append(_:), appendContentsOf(_:) » insert(_:), insertContentsOf(_:) » removeAtIndex(_:), removeRange(_:)
» removeAll()
demo
None
None
This session 1. The “Problem” 2. How Swift's collections work
3. New approaches
demo
SwiftDoc.org
None
Thank you! Nate Cook @nnnnnnnn