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
65
0
Share
Collections in Swift
Talk at CocoaConf Chicago, March 2016
Nate Cook
March 31, 2016
More Decks by Nate Cook
See All by Nate Cook
Swift's Pointy Bits
natecook1000
0
590
Flexible Code for Generic Collections
natecook1000
8
1.2k
SwiftDoc.org Preview
natecook1000
0
290
Other Decks in Programming
See All in Programming
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
450
「OSSがあるなら自作するな」は AI時代も正しいか ── Build vs Adopt の新しい判断基準
kumorn5s
7
2.7k
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
130
KMP × Kotlin 2.3 - How Android Got Slower While iOS Builds Improved by 47%
rio432
0
200
Import assertionsが消えた日~ECMAScriptの仕様はどう決まり、なぜ覆るのか~
bicstone
2
180
Making the RBS Parser Faster
soutaro
0
720
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
460
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
3
350
(Re)make Regexp in Ruby: Democratizing internals for the JIT
makenowjust
3
1.1k
Agentic UI in the Frontend: Architectures with Open Standards @JAX 2026 in Mainz
manfredsteyer
PRO
0
110
【ディップ|26年新卒研修資料】OpenAPI/Swagger REST API研修
dip_tech
PRO
0
160
AIと共に生きる技術選定 2026
sgash708
0
140
Featured
See All Featured
Leo the Paperboy
mayatellez
7
1.8k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
190
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
340
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
170
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
1.4k
How to Ace a Technical Interview
jacobian
281
24k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
190
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
150
Testing 201, or: Great Expectations
jmmastey
46
8.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