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
モダンなイニシャライザ
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Elvis Shi
September 05, 2017
Programming
970
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
モダンなイニシャライザ
モダンなイニシャライザの作り方
Elvis Shi
September 05, 2017
More Decks by Elvis Shi
See All by Elvis Shi
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
490
ゼロから始めるPreferenceの実装 / Let's implement Preferences from scratch
lovee
0
150
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
390
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
750
How did I build an Open-Source SwiftUI Toast Library
lovee
1
170
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
1.3k
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
lovee
1
380
Observation のあれこれ / A brief introduction about Observation
lovee
3
440
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
27
9k
Other Decks in Programming
See All in Programming
Claude Team Plan導入・ガイド
tk3fftk
0
190
AI時代、エンジニアはどう育つのか -未経験エンジニアの成長を間近で見て考えたこと-
thasu0123
0
110
Creating Composable Callables in Contemporary C++
rollbear
0
210
20260623_Loop Engineeringで自分の分身の問い合わせBotを作る
ryugen04
0
210
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
320
symfony/aiとlaravel/boost
77web
0
130
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
220
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
150
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
1
270
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
250
自作OSでスライド発表する
uyuki234
1
3.8k
霧の中の代数的エフェクト
funnyycat
1
370
Featured
See All Featured
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
650
Joys of Absence: A Defence of Solitary Play
codingconduct
1
410
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
630
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
It's Worth the Effort
3n
188
29k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.4k
Rails Girls Zürich Keynote
gr2m
96
14k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
200
Leo the Paperboy
mayatellez
8
1.9k
Transcript
ϞμϯͳΠχγϟϥΠβ for αϙʔλʔζCoLab
override init() { super.init() emplyedBy = "MAGES. 5pb. Game div"
job = "iOS Developer" twitter = "@lovee" qiita = "@lovee" github = "el-hoshino" additionalInfo = "͍ͨͩ·ઈࢍస৬׆ಈதʘ(^o^)ʗ" class Speaker: Developer { } }
ΠχγϟϥΠβʔɺ ͬͯ·͔͢ʁ
ҰൠతͳΠχγϟϥΠβʔ ‣ Int("10") ‣ CGPoint(x: 10, y: 20) ‣ UILabel(frame:
.zero) ‣ etc…
͜ΜͳΠχγϟϥΠζɺͨ͜͠ͱͳ͍͔ʁ let label = UILabel(frame: .zero) label.font = UIFont.systemFont(ofSize: 20)
label.textAlignment = .center label.textColor = .blue label.numberOfLines = 1 // ...
͜ΜͳΠχγϟϥΠζɺͨ͜͠ͱͳ͍͔ʁ let label = UILabel(frame: .zero) label.font = UIFont.systemFont(ofSize: 20)
label.textAlignment = .center label.textColor = .blue label.numberOfLines = 1 // ... ໘͍͘͞ !
͜ΜͳΠχγϟϥΠζɺͨ͜͠ͱͳ͍͔ʁ let labelA = UILabel(frame: .zero) labelA.font = UIFont.systemFont(ofSize: 20)
labelA.textAlignment = .center labelA.textColor = .blue labelA.numberOfLines = 1 // ... let labelB = UILabel(frame: .zero) labelB.font = UIFont.systemFont(ofSize: 14) labelB.textAlignment = .center labelB.textColor = .gray labelA.numberOfLines = 0 labelB.lineBreakMode = .byCharWrapping // ...
͜ΜͳΠχγϟϥΠζɺͨ͜͠ͱͳ͍͔ʁ let labelA = UILabel(frame: .zero) labelA.font = UIFont.systemFont(ofSize: 20)
labelA.textAlignment = .center labelA.textColor = .blue labelA.numberOfLines = 1 // ... let labelB = UILabel(frame: .zero) labelB.font = UIFont.systemFont(ofSize: 14) labelB.textAlignment = .center labelB.textColor = .gray labelA.numberOfLines = 0 labelB.lineBreakMode = .byCharWrapping // ... ม໊ ؒҧ͑ͨ !
ΠχγϟϥΠζͱ ॳظઃఆΛ ҰͰΓ͍ͨʂ
ඪ ‣ ҰͰੜͱઃఆΛऴΘΒͤΔ ‣ ಉ͡Ωʔϫʔυ 2 ճΘͳ͍ ‣ IDEʢXcodeʣͷิΛ࠷େݶʹར༻
ཧͳΠχγϟϥΠβʔ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize:
20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } ҰͰੜͱઃఆͱೖ͕Ͱ͖Δ ಉ͡୯ޠΛ ෳճॻ͔ͳ͍ ϝιουνΣʔϯʹΑΔ खܰͳิ
ཧͳΠχγϟϥΠβʔ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize:
20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } ҰͰੜͱઃఆͱೖ͕Ͱ͖Δ ಉ͡୯ޠΛ ෳճॻ͔ͳ͍ ϝιουνΣʔϯʹΑΔ खܰͳิ Ͳ͏͢Ε͜Μͳ ཧతͳΠχγϟϥΠβʔ͕ ࡞ΕΔͷ͔ͳ"
ཧͳΠχγϟϥΠβʔ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize:
20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } ҰͰੜͱઃఆͱೖ͕Ͱ͖Δ ಉ͡୯ޠΛ ෳճॻ͔ͳ͍ ϝιουνΣʔϯʹΑΔ खܰͳิ
ҰൠతͳϝιουνΣʔϯ struct ChainableType { func doSomething() -> ChainableType { //
return ... } func doAnotherThing() -> ChainableType { // return ... } // func ... }
ҰൠతͳϝιουνΣʔϯ struct ChainableType { func doSomething() -> ChainableType { //
return ... } func doAnotherThing() -> ChainableType { // return ... } // func ... } let object = ChainableType() .doSomething() .doAnotherThing() // .do...
ҰൠతͳϝιουνΣʔϯ struct ChainableType { func doSomething() -> ChainableType { //
return ... } func doAnotherThing() -> ChainableType { // return ... } // func ... } let object = ChainableType() .doSomething() .doAnotherThing() // .do... object.doSomething() ੜ࣌Ҏ֎Ͱ νΣʔϯϝιου͕ݺͯ͠·͏!
"ੜ࣌ʹ ϝιουνΣʔϯΛ ͍͍͕ͨɺ ੜ͞ΕͨΦϒδΣΫτ ϝιουνΣʔϯΛ ͑ͯཉ͘͠ͳ͍
ੜ࣌ʹ͚ͩ͏ܕΛ ࡞ͬͯ͠·͍͍͑#
APIs in UILabelMaker public struct UILabelMaker { } extension UILabelMaker
{ public func setFont(to font: UIFont) -> UILabelMaker public func setTextColor(to color: UIColor) -> UILabelMaker public func setTextAlignment(to alignment: NSTextAlignment) -> UILabelMaker public func setNumberOfLines(to number: Int) -> UILabelMaker public func setLineBreakMode(to mode: NSLineBreakMode) -> UILabelMaker } extension UILabel { public static func make(withFrame frame: CGRect = default, by making: (UILabelMaker) -> UILabelMaker) -> UILabel }
APIs in UILabelMaker public struct UILabelMaker { } extension UILabelMaker
{ public func setFont(to font: UIFont) -> UILabelMaker public func setTextColor(to color: UIColor) -> UILabelMaker public func setTextAlignment(to alignment: NSTextAlignment) -> UILabelMaker public func setNumberOfLines(to number: Int) -> UILabelMaker public func setLineBreakMode(to mode: NSLineBreakMode) -> UILabelMaker } extension UILabel { public static func make(withFrame frame: CGRect = default, by making: (UILabelMaker) -> UILabelMaker) -> UILabel } ʗਓ◕ ‿‿ ◕人ʘϫέΨϫΧϥφΠϤ
APIs in UILabelMaker public struct UILabelMaker { } extension UILabelMaker
{ public func setFont(to font: UIFont) -> UILabelMaker public func setTextColor(to color: UIColor) -> UILabelMaker public func setTextAlignment(to alignment: NSTextAlignment) -> UILabelMaker public func setNumberOfLines(to number: Int) -> UILabelMaker public func setLineBreakMode(to mode: NSLineBreakMode) -> UILabelMaker } extension UILabel { public static func make(withFrame frame: CGRect = default, by making: (UILabelMaker) -> UILabelMaker) -> UILabel } ϝιου make(withFrame: by:) ͷΓ ΫϩʔδϟʔҾ making ͷܕ UILabelMaker ͷೖྗΛ͔͢Β ࣮ߦͯ͠ UILabelMaker ͷग़ྗΛग़ؔ͢Λ Ҿ making ͱ͍ͯͯͩ͘͠͠͞
APIs in UILabelMaker public struct UILabelMaker { } extension UILabelMaker
{ public func setFont(to font: UIFont) -> UILabelMaker public func setTextColor(to color: UIColor) -> UILabelMaker public func setTextAlignment(to alignment: NSTextAlignment) -> UILabelMaker public func setNumberOfLines(to number: Int) -> UILabelMaker public func setLineBreakMode(to mode: NSLineBreakMode) -> UILabelMaker } extension UILabel { public static func make(withFrame frame: CGRect = default, by making: (UILabelMaker) -> UILabelMaker) -> UILabel } making ͷೖྗͱͯ͠ͷ UILabelMakerɻ making ʹ͢ඞཁ͕͋ΔͷͰɺ UILabel ͔Β UILabelMaker ʹ ੜ͢ΔΠχγϟϥΠβ͕ඞཁ making ͷग़ྗͱͯ͠ͷ UILabelMakerɻ ࠷ऴతʹ UILabel ͕ඞཁͳͷͰɺ UILabelMaker ͔Β UILabel Λ ग़ྗ͢Δϝιου͕ඞཁ
APIs in UILabelMaker public struct UILabelMaker { } extension UILabelMaker
{ public func setFont(to font: UIFont) -> UILabelMaker public func setTextColor(to color: UIColor) -> UILabelMaker public func setTextAlignment(to alignment: NSTextAlignment) -> UILabelMaker public func setNumberOfLines(to number: Int) -> UILabelMaker public func setLineBreakMode(to mode: NSLineBreakMode) -> UILabelMaker } extension UILabel { public static func make(withFrame frame: CGRect = default, by making: (UILabelMaker) -> UILabelMaker) -> UILabel } ͦͦ UILabelMaker Λ௨ͯ͡ UILabel Λઃఆ͍ͯ͠ΔͷͰɺ UILabel ΛϓϩύςΟʔͱͯ͠ อ࣋͢Δඞཁ͕͋Δ
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabelMaker { public func setFont(to font: UIFont) -> UILabelMaker public func setTextColor(to color: UIColor) -> UILabelMaker public func setTextAlignment(to alignment: NSTextAlignment) -> UILabelMaker public func setNumberOfLines(to number: Int) -> UILabelMaker public func setLineBreakMode(to mode: NSLineBreakMode) -> UILabelMaker } extension UILabel { public static func make(withFrame frame: CGRect = default, by making: (UILabelMaker) -> UILabelMaker) -> UILabel }
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabelMaker { public func setFont(to font: UIFont) -> UILabelMaker { self.label.font = font return self } public func setTextColor(to color: UIColor) -> UILabelMaker { self.label.textColor = color return self } public func setTextAlignment(to alignment: NSTextAlignment) -> UILabelMaker { self.label.textAlignment = alignment return self } public func setNumberOfLines(to number: Int) -> UILabelMaker { self.label.numberOfLines = number return self } public func setLineBreakMode(to mode: NSLineBreakMode) -> UILabelMaker { self.label.lineBreakMode = mode return self } }
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } }
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) -> UILabelMaker in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { (maker) in return maker .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { return $0 .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero, by: { $0 .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) })
APIs in UILabelMaker public struct UILabelMaker { let label: UILabel
} extension UILabel { public static func make(withFrame frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel.make(withFrame: .zero) { $0 .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) }
ཧͳΠχγϟϥΠβʔ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize:
20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } // Ռ let label = UILabel.make(withFrame: .zero) { $0 .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } ໊ؔ͗͢
Tricks in Initializers class SomeClass { var someInt: Int init(someInt:
Int) { self.someInt = someInt } } let someClass = SomeClass() //Error: Missing arguments
Tricks in Initializers class SomeClass { var someInt: Int init(someInt:
Int) { self.someInt = someInt } } extension SomeClass { static func `init`() -> SomeClass { return SomeClass(someInt: 0) } } let someClass = SomeClass() //someClass.someInt == 0
APIs in UILabelMaker public struct UILabelMaker { fileprivate let label:
UILabel } extension UILabel { public static func `init`(frame: CGRect = .zero, by making: (UILabelMaker) -> UILabelMaker) -> UILabel { let label = UILabel(frame: frame) let maker = UILabelMaker(label: label) let resultMaker = making(maker) let resultLabel = resultMaker.label return resultLabel } } let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) }
ཧͳΠχγϟϥΠβʔ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize:
20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } // Ռ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize: 20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } શʹҰக$
ΧΠθϯ
ΧΠθϯ ‣ Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ ‣ ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ ‣ ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ
Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ public struct Maker<Product> { let product: Product
} extension Maker where Product: UILabel { public func setFont(to font: UIFont) -> Maker public func setTextColor(to color: UIColor) -> Maker public func setTextAlignment(to alignment: NSTextAlignment) -> Maker public func setNumberOfLines(to number: Int) -> Maker public func setLineBreakMode(to mode: NSLineBreakMode) -> Maker } extension UILabel { public static func `init`(frame: CGRect = .zero, by making: (Maker<UILabel>) -> Maker<UILabel>) -> UILabel }
Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ public struct Maker<Product> { let product: Product
} extension Maker where Product: UILabel { public func setFont(to font: UIFont) -> Maker public func setTextColor(to color: UIColor) -> Maker public func setTextAlignment(to alignment: NSTextAlignment) -> Maker public func setNumberOfLines(to number: Int) -> Maker public func setLineBreakMode(to mode: NSLineBreakMode) -> Maker } extension UILabel { public static func `init`(frame: CGRect = .zero, by making: (Maker<UILabel>) -> Maker<UILabel>) -> UILabel } ܕ͝ͱͷઃఆϝιου͚ͩ ࡞Εྑ͍
ΧΠθϯ ‣ Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ ‣ ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ ‣ ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ
ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ Framework Λ ࡞Δฤ
ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ
ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ
ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ Framework Λ ͏ฤ
ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ
ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ
ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ
ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ https://speakerdeck.com/lovee/carthage-woshi-tutafei-opunnashe-nei-kai-fa ؔ࿈ςʔϚɿ ࣗ࡞ Framework Λ Carthage ʹରԠ͢Δ
ΧΠθϯ ‣ Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ ‣ ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ ‣ ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ
ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ Targeted Extensions
ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ http://tech.vasily.jp/entry/swift_modern_extensions
ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ http://tech.vasily.jp/entry/swift_modern_extensions
ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ public struct MakerType<Package> { let package: Package public init(_
package: Package) { self.package = package } } public protocol MakerTypeCompatible { } extension MakerTypeCompatible { public typealias MKR = MakerType<Self> public var mkr: MKR { return MakerType(self) } }
ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ public struct MakerType<Package> { let package: Package public init(_
package: Package) } public protocol MakerTypeCompatible { } extension MakerTypeCompatible { public typealias MKR = MakerType<Self> public var mkr: MKR } extension UILabel: MakerTypeCompatible { } public extension MakerType where Package == UILabel { public static func `init`(frame: CGRect = .zero, by making: (Maker<UILabel>) -> Maker<UILabel>) -> UILabel { let label = UILabel(frame: frame) let maker = Maker(product: label) let resultMaker = making(maker) let resultLabel = resultMaker.product return resultLabel } }
ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ public struct MakerType<Package> { let package: Package public init(_
package: Package) } public protocol MakerTypeCompatible { } extension MakerTypeCompatible { public typealias MKR = MakerType<Self> public var mkr: MKR } extension UILabel: MakerTypeCompatible { } public extension MakerType where Package == UILabel { public static func `init`(frame: CGRect = .zero, by making: (Maker<UILabel>) -> Maker<UILabel>) -> UILabel } let label = UILabel.MKR(frame: .zero) { $0 .setNumberOfLines(to: 0) .setTextColor(to: .blue) }
ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ public struct MakerType<Package> { let package: Package public init(_
package: Package) } public protocol MakerTypeCompatible { } extension MakerTypeCompatible { public typealias MKR = MakerType<Self> public var mkr: MKR } extension UILabel: MakerTypeCompatible { } public extension MakerType where Package == UILabel { public static func `init`(frame: CGRect = .zero, by making: (Maker<UILabel>) -> Maker<UILabel>) -> UILabel } let label = UILabel.MKR(frame: .zero) { $0 .setNumberOfLines(to: 0) .setTextColor(to: .blue) }
ΧΠθϯ ‣ Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ ‣ ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗͍͍ͯͨ͠ʂ ‣ ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ
DEMO