Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Design patterns in Swift Vol. 0
Search
Johnlin
December 13, 2016
Programming
130
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Design patterns in Swift Vol. 0
Johnlin
December 13, 2016
More Decks by Johnlin
See All by Johnlin
用 MLIR 實作 一個 Ruby IR (intermediate representation)
johnlinvc
0
390
Unearth Ruby builtin Gems 發掘 Ruby 的內建 Gems
johnlinvc
0
490
Ruby 型別檢查工具簡介
johnlinvc
0
700
Swift Actor 實作探索
johnlinvc
0
230
用 mruby 來寫跨平台工具
johnlinvc
0
150
Actor model 簡介
johnlinvc
0
280
一起玩 Helm 3
johnlinvc
1
200
為什麼 App 卡卡的
johnlinvc
2
1.4k
如何使用 byebug 來除錯 Ruby 程式
johnlinvc
0
300
Other Decks in Programming
See All in Programming
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
180
contenteditable と日本語入力に向き合う
colorful12
0
200
Compiling a real-world CLI tool to native with Static Hermes
tmikov2023
0
120
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
350
残高管理から台帳サービスへの進化
artoy
1
200
TechTown - Creating Composable Callables in Contemporary C++
rollbear
0
120
Kotlinの15年とServer-Side Kotlin
n_takehata
1
150
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
420
Next.js: The final boss of Web Performance
guaca
0
230
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
140
Jetpack Compose Mechanisms
skydoves
3
810
Featured
See All Featured
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
380
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Leo the Paperboy
mayatellez
10
2.4k
Building AI with AI
inesmontani
PRO
1
1.3k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
RailsConf 2023
tenderlove
30
1.6k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
500
Optimizing for Happiness
mojombo
378
71k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
280
BBQ
matthewcrist
90
10k
Transcript
Design Patterns in Swift John Lin Vol 0
ॄኄੋDesign patterns? • Design patterns = ઃܭࣜɻ • ֤छೈᱪதৗݟతՍߏɼ㢨ଞ၇औҰݸ໊ࣈɻṜᒬࢠ ࠽ํศ౼ɻ
• औៃࣗݐஙላత໊ࣈɻ
࠷ஶ໊తઃܭࣜ၇ • Design Patterns: Elements of Reusable Object-Oriented Software
BY Erich Gamma , Richard Helm , Ralph Johnson , John Vlissides ؆ GoF(gang of four) ࢛ਓ㢨ɻ • ཫ໘ሜྃ23 छৗݟతC++݅ಋ ࣜɻ
ઃܭࣜత • Ṝຊॻੋඳड़C++ Smalltalk தৗݟతղܾ తํ๏ɻ • ୠੋሣଖଞޠݴိ㘸ɼṜࠣղܾํ๏҃ڐ༗ߋతղ ܾํ๏ɻ
Singleton • ݸఔࣜཫೳ༗Ұݸመᱪ(instance)ɻ • ৗ༻ိᙛ࡞ࢿݯཧऀ(manager)ɻ • መ࡞࣌धཁߟྀଟࣥߦॹ࣌త㐫گɻ
@interface Singleton : NSObject @end @implementation Singleton + (instancetype)sharedInstance {
static Singleton *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[Singleton alloc] init]; }); return sharedInstance; } @end
error: 'dispatch_once_t' is unavailable in Swift: Use lazily initialized globals
instead static var onceToken: dispatch_once_t = 0 ^~~~~~~~~~~~~~~
class Singleton { static let sharedInstance = Singleton() }
Iterator • భث • ༻ိଘऔҰݸ༰ث㚎తݩૉ • ෆधཁ༻ index
for i in 1...10 { print(i) } for c in
"hello world".characters { print(c) }
Swift తభث Protocol IteratorProtocol • ੋҰݸ Protocol • ༗ҰݸඞཁతവᏐ next()
• ᙛ༗ԼҰݸݩૉ࣌ɼճၚಹݸݩૉ • ᔒ༗ԼҰݸݩૉత࣌ީɼճၚ nil
Swift తجૅభث AnyIterator • ੋҰݸ Struct • আྃభ೭֎ؐ༗ଖଞతޭೳɻ
var s = "" let iter = AnyIterator<String> { let
c = s.characters.count % 2 == 0 ? "5" : "6" s = s + c return s.characters.count > 10 ? nil : s } for c in iter { print(c) } /* 5 56 … 565656565 5656565656 */
var s = "" let iter = AnyIterator<String> { let
c = s.characters.count % 2 == 0 ? "5" : "6" s = s + c return s.characters.count > 10 ? nil : s } for c in iter.filter({ $0.characters.count % 2 == 0 }) { print(c) } /* 56 5656 565656 56565656 5656565656 */
var s = "" let iter = AnyIterator<String> { let
c = s.characters.count % 2 == 0 ? "5" : "6" s = s + c return s.characters.count > 10 ? nil : s } for (idx,c) in iter.enumerated() { print("\(idx):\(c)") } /* 0:5 1:56 2:565 … 8:565656565 9:5656565656 */
ᩋ㟬తClass ೳඃభ • ঝ IteratorProtocol Sequence • typealias Element
= <ཁճၚతType> • ఆٛ next()
class PPAP : Sequence, IteratorProtocol { typealias Element = String
let stuffs = [ "APPLEPEN", "PINEAPPLEPEN", "PENPINEAPPLEAPPLEPEN"] var idx = 0 func next()-> String? { defer {idx += 1} guard idx < stuffs.count else { return nil } return stuffs[idx] } } let ppap = PPAP() for p in ppap { print(p) }
class PPAP : Sequence, IteratorProtocol { typealias Element = String
let stuffs = [ "APPLEPEN", "PINEAPPLEPEN", "PENPINEAPPLEAPPLEPEN"] var idx = 0 func next()-> String? { defer {idx += 1} guard idx < stuffs.count else { return nil } return stuffs[idx] } } let ppap = PPAP() for p in ppap { print(p) } /* APPLEPEN PINEAPPLEPEN PENPINEAPPLEAPPLEPEN */
let ppap = PPAP() for p in ppap { print(p)
}
Q&A