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
モダンなイニシャライザ
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Elvis Shi
September 05, 2017
Programming
980
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
580
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
570
ゼロから始めるPreferenceの実装 / Let's implement Preferences from scratch
lovee
0
160
Kotlin エンジニアへ送る:Swift 案件に参加させられる日に備えて~似てるけど色々違う Swift の仕様 / from Kotlin to Swift
lovee
1
410
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
790
How did I build an Open-Source SwiftUI Toast Library
lovee
1
190
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
460
Other Decks in Programming
See All in Programming
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
1.8k
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.9k
Verilogで学ぶCPU自作入門
uyuki234
8
4.1k
Railsバージョンを4倍にした話
optim
0
310
SREの越境 / SRE Collaboration
y0hgi
2
310
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
1k
Next.js: The final boss of Web Performance
guaca
0
230
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
390
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
6.6k
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
150
phperだってパンチカードでコーディングしたい
o0h
PRO
0
330
Featured
See All Featured
A Tale of Four Properties
chriscoyier
163
24k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
490
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
300
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
260
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
570
It's Worth the Effort
3n
188
29k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
800
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
360
Intergalactic Javascript Robots from Outer Space
tanoku
273
28k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
280
Building an army of robots
kneath
307
47k
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