モダンなイニシャライザ

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
September 05, 2017

 モダンなイニシャライザ

モダンなイニシャライザの作り方

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

September 05, 2017
Tweet

Transcript

  1. ϞμϯͳΠχγϟϥΠβ for αϙʔλʔζCoLab

  2. override init() { super.init() emplyedBy = "MAGES. 5pb. Game div"

    job = "iOS Developer" twitter = "@lovee" qiita = "@lovee" github = "el-hoshino" additionalInfo = "͍ͨͩ·ઈࢍస৬׆ಈதʘ(^o^)ʗ" class Speaker: Developer { } }
  3. ΠχγϟϥΠβʔɺ ࢖ͬͯ·͔͢ʁ

  4. ҰൠతͳΠχγϟϥΠβʔ ‣ Int("10") ‣ CGPoint(x: 10, y: 20) ‣ UILabel(frame:

    .zero) ‣ etc…
  5. ͜ΜͳΠχγϟϥΠζɺͨ͜͠ͱͳ͍͔ʁ let label = UILabel(frame: .zero) label.font = UIFont.systemFont(ofSize: 20)

    label.textAlignment = .center label.textColor = .blue label.numberOfLines = 1 // ...
  6. ͜ΜͳΠχγϟϥΠζɺͨ͜͠ͱͳ͍͔ʁ let label = UILabel(frame: .zero) label.font = UIFont.systemFont(ofSize: 20)

    label.textAlignment = .center label.textColor = .blue label.numberOfLines = 1 // ... ໘౗͍͘͞
 !
  7. ͜ΜͳΠχγϟϥΠζɺͨ͜͠ͱͳ͍͔ʁ 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 // ...
  8. ͜ΜͳΠχγϟϥΠζɺͨ͜͠ͱͳ͍͔ʁ 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 // ... ม਺໊ ؒҧ͑ͨ
 !
  9. ΠχγϟϥΠζͱ ॳظઃఆΛ Ұ౓Ͱ΍Γ͍ͨʂ

  10. ໨ඪ ‣ Ұ౓Ͱੜ੒ͱઃఆΛऴΘΒͤΔ ‣ ಉ͡Ωʔϫʔυ͸ 2 ճ΋࢖Θͳ͍ ‣ IDEʢXcodeʣͷิ׬Λ࠷େݶʹར༻

  11. ཧ૝ͳΠχγϟϥΠβʔ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize:

    20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } Ұ౓Ͱੜ੒ͱઃఆͱ୅ೖ͕Ͱ͖Δ ಉ͡୯ޠΛ
 ෳ਺ճॻ͔ͳ͍ ϝιουνΣʔϯʹΑΔ खܰͳิ׬
  12. ཧ૝ͳΠχγϟϥΠβʔ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize:

    20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } Ұ౓Ͱੜ੒ͱઃఆͱ୅ೖ͕Ͱ͖Δ ಉ͡୯ޠΛ
 ෳ਺ճॻ͔ͳ͍ ϝιουνΣʔϯʹΑΔ खܰͳิ׬ Ͳ͏͢Ε͹͜Μͳ
 ཧ૝తͳΠχγϟϥΠβʔ͕
 ࡞ΕΔͷ͔ͳ"
  13. ཧ૝ͳΠχγϟϥΠβʔ let label = UILabel(frame: .zero) { $0 .setFont(to: .systemFont(ofSize:

    20)) .setTextAlignment(to: .center) .setTextColor(to: .blue) .setNumberOfLines(to: 1) } Ұ౓Ͱੜ੒ͱઃఆͱ୅ೖ͕Ͱ͖Δ ಉ͡୯ޠΛ
 ෳ਺ճॻ͔ͳ͍ ϝιουνΣʔϯʹΑΔ खܰͳิ׬
  14. ҰൠతͳϝιουνΣʔϯ struct ChainableType { func doSomething() -> ChainableType { //

    return ... } func doAnotherThing() -> ChainableType { // return ... } // func ... }
  15. ҰൠతͳϝιουνΣʔϯ struct ChainableType { func doSomething() -> ChainableType { //

    return ... } func doAnotherThing() -> ChainableType { // return ... } // func ... } let object = ChainableType() .doSomething() .doAnotherThing() // .do...
  16. ҰൠతͳϝιουνΣʔϯ struct ChainableType { func doSomething() -> ChainableType { //

    return ... } func doAnotherThing() -> ChainableType { // return ... } // func ... } let object = ChainableType() .doSomething() .doAnotherThing() // .do... object.doSomething() ੜ੒࣌Ҏ֎Ͱ΋ νΣʔϯϝιου͕ݺ΂ͯ͠·͏!
  17. "ੜ੒࣌ʹ ϝιουνΣʔϯΛ ࢖͍͍͕ͨɺ 
 ੜ੒͞ΕͨΦϒδΣΫτ͸ ϝιουνΣʔϯΛ ࢖͑ͯཉ͘͠ͳ͍

  18. ੜ੒࣌ʹ͚ͩ࢖͏ܕΛ
 ࡞ͬͯ͠·͑͹͍͍#

  19. 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 }
  20. 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 } ʗਓ◕ ‿‿ ◕人ʘϫέΨϫΧϥφΠϤ
  21. 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 ͱͯ͠౉͍ͯͩ͘͠͞
  22. 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 Λ
 ग़ྗ͢Δϝιου͕ඞཁ
  23. 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 ΛϓϩύςΟʔͱͯ͠
 อ࣋͢Δඞཁ͕͋Δ
  24. 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 }
  25. 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 } }
  26. 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 } }
  27. 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) })
  28. 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) })
  29. 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) })
  30. 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) })
  31. 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) })
  32. 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) })
  33. 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) })
  34. 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) })
  35. 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) })
  36. 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) })
  37. 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) })
  38. 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) })
  39. 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) })
  40. 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) })
  41. 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) })
  42. 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) })
  43. 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) })
  44. 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) })
  45. 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) }
  46. ཧ૝ͳΠχγϟϥΠβʔ 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) } ؔ਺໊௕͗͢
  47. Tricks in Initializers class SomeClass { var someInt: Int init(someInt:

    Int) { self.someInt = someInt } } let someClass = SomeClass() //Error: Missing arguments
  48. 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
  49. 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) }
  50. ཧ૝ͳΠχγϟϥΠβʔ 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) } ׬શʹҰக$
  51. ΧΠθϯ

  52. ΧΠθϯ ‣ Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ ‣ ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ ‣ ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ

  53. Կ͔ͷܕ͕ग़Δͨͼʹ 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 }
  54. Կ͔ͷܕ͕ग़Δͨͼʹ 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 } ܕ͝ͱͷઃఆϝιου͚ͩ
 ࡞Ε͹ྑ͍
  55. ΧΠθϯ ‣ Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ ‣ ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ ‣ ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ

  56. ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ Framework Λ
 ࡞Δฤ

  57. ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ

  58. ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ

  59. ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ Framework Λ
 ࢖͏ฤ

  60. ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ

  61. ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ

  62. ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ

  63. ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ https://speakerdeck.com/lovee/carthage-woshi-tutafei-opunnashe-nei-kai-fa ؔ࿈ςʔϚɿ
 ࣗ࡞ Framework Λ Carthage ʹରԠ͢Δ

  64. ΧΠθϯ ‣ Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ ‣ ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ ‣ ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ

  65. ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ Targeted Extensions

  66. ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ http://tech.vasily.jp/entry/swift_modern_extensions

  67. ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ http://tech.vasily.jp/entry/swift_modern_extensions

  68. ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ 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) } }
  69. ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ 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 } }
  70. ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ 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) }
  71. ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ 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) }
  72. ΧΠθϯ ‣ Կ͔ͷܕ͕ग़Δͨͼʹ XxxMaker ࡞ΔͷͩΔ͍ʂ ‣ ΋ͬͱͨ͘͞ΜͷϓϩδΣΫτͰڞ༗ͯ͠࢖͍͍ͨʂ ‣ ϑϨʔϜϫʔΫͰͷϝιουͷ໊લিಥΛճආ͍ͨ͠ʂ

  73. DEMO