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
35
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
460
Flexible Code for Generic Collections
natecook1000
8
1.2k
SwiftDoc.org Preview
natecook1000
0
270
Other Decks in Programming
See All in Programming
Compose でデザインと実装の差異を減らすための取り組み
oidy
1
300
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
4
370
なぜイベント駆動が必要なのか - CQRS/ESで解く複雑系システムの課題 -
j5ik2o
7
2.5k
DROBEの生成AI活用事例 with AWS
ippey
0
130
WebDriver BiDiとは何なのか
yotahada3
1
140
[JAWS-UG横浜 #79] re:Invent 2024 の DB アップデートは Multi-Region!
maroon1st
1
140
Conform を推す - Advocating for Conform
mizoguchicoji
3
680
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
120
Unity Android XR入門
sakutama_11
0
140
CloudNativePGがCNCF Sandboxプロジェクトになったぞ! 〜CloudNativePGの仕組みの紹介〜
nnaka2992
0
220
SRE、開発、QAが協業して挑んだリリースプロセス改革@SRE Kaigi 2025
nealle
3
4.1k
定理証明プラットフォーム lapisla.net
abap34
1
1.7k
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1030
460k
Facilitating Awesome Meetings
lara
51
6.2k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
We Have a Design System, Now What?
morganepeng
51
7.4k
Unsuck your backbone
ammeep
669
57k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
400
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Statistics for Hackers
jakevdp
797
220k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
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