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
Design patterns in Swift Vol. 0
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Johnlin
December 13, 2016
Programming
120
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
290
Unearth Ruby builtin Gems 發掘 Ruby 的內建 Gems
johnlinvc
0
370
Ruby 型別檢查工具簡介
johnlinvc
0
610
Swift Actor 實作探索
johnlinvc
0
200
用 mruby 來寫跨平台工具
johnlinvc
0
120
Actor model 簡介
johnlinvc
0
240
一起玩 Helm 3
johnlinvc
1
160
為什麼 App 卡卡的
johnlinvc
2
1.3k
如何使用 byebug 來除錯 Ruby 程式
johnlinvc
0
260
Other Decks in Programming
See All in Programming
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
220
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
720
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
320
Inside Stream API
skrb
1
690
JavaDoc 再入門
nagise
0
320
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
3.7k
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
2
600
New "Type" system on PicoRuby
pocke
1
840
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
220
Oxlintのカスタムルールの現況
syumai
6
1.1k
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
3
1.3k
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
10
3.5k
Featured
See All Featured
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
560
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
New Earth Scene 8
popppiees
3
2.3k
Producing Creativity
orderedlist
PRO
348
40k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
230
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.5k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.7k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
71
40k
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