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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Nate Cook
March 31, 2016
Programming
0
62
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
560
Flexible Code for Generic Collections
natecook1000
8
1.2k
SwiftDoc.org Preview
natecook1000
0
290
Other Decks in Programming
See All in Programming
AIに任せる範囲を安全に広げるためにやっていること
fukucheee
0
130
[SF Ruby Feb'26] The Silicon Heel
palkan
0
110
文字コードの話
qnighy
44
17k
S3ストレージクラスの「見える」「ある」「使える」は全部違う ─ 体験から見た、仕様の深淵を覗く
ya_ma23
0
580
GoのDB アクセスにおける 「型安全」と「柔軟性」の両立 - Bob という選択肢
tak848
0
110
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
600
maplibre-gl-layers - 地図に移動体たくさん表示したい
kekyo
PRO
0
270
encoding/json/v2のUnmarshalはこう変わった:内部実装で見る設計改善
kurakura0916
0
420
米国のサイバーセキュリティタイムラインと見る Goの暗号パッケージの進化
tomtwinkle
2
590
CSC307 Lecture 15
javiergs
PRO
0
250
PostgreSQL を使った快適な go test 環境を求めて
otakakot
0
560
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
340
Featured
See All Featured
For a Future-Friendly Web
brad_frost
183
10k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
150
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
180
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
180
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Mobile First: as difficult as doing things right
swwweet
225
10k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
110
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
290
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
470
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.1k
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