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
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
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
540
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
530
ゼロから始めるPreferenceの実装 / Let's implement Preferences from scratch
lovee
0
160
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
400
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
770
How did I build an Open-Source SwiftUI Toast Library
lovee
1
180
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
390
Observation のあれこれ / A brief introduction about Observation
lovee
3
450
Other Decks in Programming
See All in Programming
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
3
1k
バグを直したら useEffect が消えた
colorful12
3
840
自分的「カンファレンスの楽しみ方」
syumai
0
190
ソフトウェアエンジニアにとっての生成AI - 特性を知って使い倒す / generative ai for software enginner
kishida
7
2.3k
不幸な GC
chencmd
0
880
ソフトウェアラスタライザ
fadis
1
790
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.9k
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
220
『寄り添うラジオ』をAIで作る 体験価値から逆算した、会話しないUXと品質設計
theoriatec2024
2
110
Swift愛好会100回記念 第1回を振り返る
jollyjoester
0
120
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
680
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
840
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
From π to Pie charts
rasagy
0
350
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
4
580
[SF Ruby Conf 2025] Rails X
palkan
2
1.3k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
970
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
810
A Soul's Torment
seathinner
7
3.5k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
680
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