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
Transcript
20 slides
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
More Decks by Johnlin
See All by Johnlin
用 MLIR 實作 一個 Ruby IR (intermediate representation)
johnlinvc
0
430
Unearth Ruby builtin Gems 發掘 Ruby 的內建 Gems
johnlinvc
0
520
Ruby 型別檢查工具簡介
johnlinvc
0
730
Swift Actor 實作探索
johnlinvc
0
260
用 mruby 來寫跨平台工具
johnlinvc
0
180
Actor model 簡介
johnlinvc
0
320
一起玩 Helm 3
johnlinvc
1
230
為什麼 App 卡卡的
johnlinvc
2
1.4k
如何使用 byebug 來除錯 Ruby 程式
johnlinvc
0
320
Other Decks in Programming
See All in Programming
AIコーディング時代のKotest ユニットテストから APIレベルの仕様固定化テストへ
ooga04
0
300
LLM 101
dalinaum
0
510
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
320
Goコンパイラを作ってGoをもっと理解する / Build a Go compiler to better understand Go
ikura_hamu
0
130
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
380
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
140
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
220
RaspberryPi 5で軽くはじめるk3sとtailscaleを用いたホームサーバー構築 (1)
nasum
0
410
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
170
地方こそサーバーレス — 10年続けた、顔が見えるチームで確かな基盤を / Serverless Matters Most in Regional Areas — 10 Years of Building Reliable Foundations with Close-Knit Teams
seike460
PRO
2
500
Scalaの型でマルチテナントの認可を守る
nextbeatdev
0
160
疎結合とは何か?ソフトウェア設計における結合バランスについて考える #phpcon_niigata
kajitack
2
370
Featured
See All Featured
A Tale of Four Properties
chriscoyier
163
24k
Building Applications with DynamoDB
mza
96
7.3k
It's Worth the Effort
3n
188
29k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.5k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.9k
Utilizing Notion as your number one productivity tool
mfonobong
4
650
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
350
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
61k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
エンジニアに許された特別な時間の終わり
watany
109
250k