20171128_iOSLT_Swiftとイニシャライザ

E45f9c343d90c74554c65c89c6f861bc?s=47 shtnkgm
November 28, 2017

 20171128_iOSLT_Swiftとイニシャライザ

E45f9c343d90c74554c65c89c6f861bc?s=128

shtnkgm

November 28, 2017
Tweet

Transcript

  1. SwiftͱΠχγϟϥΠβ 2017-11-28 @snakagam / Shota Nakagami

  2. ࿩͢಺༰ 4 Swiftͷجຊతͳ಺༰ʮΠχγϟϥΠβʯʹ͍ͭͯ 4 ΠχγϟϥΠβͱܕͷؔ܎ʢΫϥεɺߏ଄ମɺྻڍମʣ 4 ΠχγϟϥΠβͷछྨͱઆ໌

  3. ࣍ͷΩʔϫʔυ͕ϞϠͬͱ͍ͯ͠Δํʹ͓͢͢Ί! 4 Failable Initilizer/ࣦഊՄೳΠχγϟϥΠβ 4 Default Initializer/طఆΠχγϟϥΠβ 4 Memberwise Initilaizer/શ߲໨ΠχγϟϥΠβ

    4 Designated Initializer/ࢦఆΠχγϟϥΠβ 4 Convenience Initializer/؆қΠχγϟϥΠβ 4 Required Initializer/ඞਢΠχγϟϥΠβ
  4. ΠχγϟϥΠβͱ͸ 4 ܕʢΫϥεɺߏ଄ମɺྻڍମʣͷΠϯελϯεΛॳظԽ ʢinitializeʣ͢Δಛघͳϝιουͷ͜ͱ 4 AppleެࣜϦϑΝϨϯεʛInitialization init() { // ॳظԽ

    }
  5. ΠχγϟϥΠβͷه๏ class MyClass { let a: Int // func͕ෆཁʢinitΩʔϫʔυͷΈʣ init(a:

    Int) { self.a = a } } let case1 = MyClass.init(a: 0) // ݺͼग़࣌͠ͷϝιου໊͕লུՄೳ let case2 = MyClass(a: 0)
  6. ͳͥॳظԽ͕ඞཁͳͷʁ ϓϩύςΟ͕ਖ਼͘͠ॳظԽͰ͖ͳ͍৔߹... 4 ܕͷ੔߹ੑ͕औΕͳ͍ 4 ϝϞϦ҆શͰͳ͍ʢϝϞϦ֬อɺॳظԽʣ

  7. Swift͸҆શ C΍ObjC 4 ڧҾʹ0΍falseɺNULLͰॳظԽʢόάͷݩʣ Swift 4 ܕͷ੔߹ੑɺॳظԽ͸ίϯύΠϥʹΑͬͯ੩తʹνΣοΫ

  8. ίϯύΠϧΤϥʔͷྫ OptionalͰͳ͍ͷʹname͸࣮࣭nilʢܕͷෆ੔߹ʣ class Book { let name: String // ॳظԽ͞Ε͍ͯͳ͍

    init(name: String) { // ίϯύΠϧΤϥʔ // return from initializer without initializing all stored properties } }
  9. SwiftΫΠζ ͲͷϓϩύςΟΛॳظԽ͢Δඞཁ͕͋Δʁ!

  10. class MyClass { let a: Int let b: Int =

    0 let c: Int? let d: Int? = 0 var e: Int var f: Int = 0 var g: Int? var h: Int? = 0 var i: Int { return 0 } var j: Int? { return 0 } init() { // ͲͷϓϩύςΟΛॳظԽ͢Δ??? } }
  11. ਖ਼ղ͸...

  12. class MyClass { let a: Int // ॳظԽ͕ඞཁʂ let b:

    Int = 0 let c: Int? // ॳظԽ͕ඞཁʂ let d: Int? = 0 var e: Int // ॳظԽ͕ඞཁʂ var f: Int = 0 var g: Int? var h: Int? = 0 var i: Int { return 0 } var j: Int? { return 0 } init() { a = 0 c = 0 e = 0 } }
  13. ΠχγϟϥΠβ಺ͰॳظԽͷཁ൱ SwiftΫΠζͷ֤ϓϩύςΟΛ෼ྨ͢Δͱɾɾɾʁ

  14. // ʢෆཁʣίϯϐϡʔςουϓϩύςΟ var i: Int { return 0 } var

    j: Int? { return 0 } // ʢෆཁʣσϑΥϧτ஋͋ΓͷετΞυϓϩύςΟ let b: Int = 0 let d: Int? = 0 var f: Int = 0 var h: Int? = 0 // ʢෆཁʣOptionalܕͷม਺ετΞυϓϩύςΟ // ˒҉໧తʹnil͕σϑΥϧτ஋ͱͯ͠ઃఆ͞ΕΔ var g: Int? // ʢඞཁʣσϑΥϧτ஋ͳ͠ɺඇΦϓγϣφϧͷม਺ετΞυϓϩύςΟ var e: Int // ʢඞཁʣσϑΥϧτ஋ͳ͠ͷఆ਺ετΞυϓϩύςΟ let a: Int let c: Int?
  15. ม਺ϓϩύςΟͷ҉໧తͳnil୅ೖ 4 σϑΥϧτ஋ͳ͠ͷOptionalͷετΞυϓϩύςΟʹؔ͠ ͯ͸ఆ਺͔ม਺͔ʹΑͬͯڍಈ͕ҟͳΔ 4 ม਺ͷ৔߹͸҉໧తʹnil͕୅ೖ͞ΕΔ 4 ఆ਺͸nil͕୅ೖ͞Εͳ͍ ʢ҉໧తʹnilΛ୅ೖͯ͠΋ɺมߋ͕Ͱ͖ͳ͍ͨΊʣ

  16. ॳظԽཁ൱Λ·ͱΊΔͱ...

  17. ॳظԽཁ൱ ϓϩύςΟछผ ิ଍આ໌ ඞཁ σϑΥϧτ஋ͳ͠ɺఆ਺ετΞυ ϓϩύςΟ - ඞཁ σϑΥϧτ஋ͳ͠ɺඇΦϓγϣφ ϧͷม਺ετΞυϓϩύςΟ

    - - Optionalܕͷม਺ετΞυϓϩ ύςΟ ҉໧తʹnilͰॳظԽ͞ΕΔ - σϑΥϧτ஋͋ΓͷετΞυϓϩ ύςΟ σϑΥϧτ஋ͰॳظԽ͞ΕΔ - ίϯϐϡʔςουϓϩύςΟ ஋Λอ࣋͠ͳ͍ͷͰॳظԽ͕ෆ ཁ
  18. Ϋϥεɾߏ଄ମɾྻڍମ ڞ௨ͷΠχγϟϥΠβ

  19. ࣦഊՄೳΠχγϟϥΠβʛFailable Initilizer 4 ॳظԽʹࣦഊ͢ΔՄೳੑͷ͋Δinitilizer 4 ໭Γ஋͸OptionalܕʢॳظԽʹࣦഊͨ͠৔߹ʹnilΛฦ͢ʣ

  20. class WebPage { let title: String let url: URL init?(title:

    String, urlString: String) { // URLจࣈྻΛURLʹม׵Ͱ͖ͳ͍৔߹͸ॳظԽΛࣦഊͤ͞Δ guard let url = URL(string: urlString) else { return nil } self.title = title self.url = url } }
  21. طఆΠχγϟϥΠβʛDefault Initializer 4 ϓϩύςΟͷॳظԽ͕ෆཁͳ৔߹ʹ҉໧తʹఆٛ͞ΕΔ

  22. class MyClass { var name: String = "MyClass" var number:

    Int = 0 // ҉໧తʹinit()͕࣮૷͞ΕΔ } let myClass = MyClass()
  23. ߏ଄ମͷΠχγϟϥΠβ

  24. શ߲໨ΠχγϟϥΠβʛMemberwise Initilaizer 4 structͰ҉໧తʹఆٛ͞ΕΔΠχγϟϥΠβ 4 ϓϩύςΟ͕ଟ͍৔߹ʹɺΠχγϟϥΠβΛ࣮૷͢Δखؒ ͕ল͚Δ

  25. struct Book { let id: String let title: String var

    price: Int // ΠχγϟϥΠβ͸࣮૷͍ͯ͠ͳ͍ } let book = Book(id: "001", title: "SwiftyBook", price: 1500)
  26. ྑ͍!!!

  27. ɺ͕ଞʹΠχγϟϥΠβͭ͘Δͱར༻ෆՄ!

  28. ɺ͕extentionͰճආՄ

  29. struct Book { let id: String let title: String var

    price: Int } // ಠࣗΠχγϟϥΠβΛextensionʹهࡌ extension Book { init() { id = "000" title = "λΠτϧͳ͠" price = 0 } } let book1 = Book(id: "001", title: "SwiftyBook", price: 1500) let book2 = Book()
  30. ͨͩ͠ΞΫηεϨϕϧ͸internal ϥΠϒϥϦԽͱ͔ͰผϞδϡʔϧʹ͢ΔͱΞΫηεͰ͖ͳ͍ͷ Ͱ஫ҙ

  31. ྻڍମͷΠχγϟϥΠβ

  32. ྻڍମͷϝϯό໊ͰॳظԽ enum͸ετΞυϓϩύςΟΛ࣋ͨͳ͍ͨΊɺॳظԽ͸؆୯ enum TrafficSignal { case green case yellow case

    red } let signal: TrafficSignal = .green
  33. ΧελϜΠχγϟϥΠβ 4 ΠχγϟϥΠβ಺Ͱɺselfʹ஋Λ୅ೖ͢Δ enum TrafficSignal { case green case yellow

    case red init() { self = .red } } // .red let signal = TrafficSignal()
  34. raw value͕ఆٛ͞Ε͍ͯΔ৔߹ ࣦഊՄೳΠχγϟϥΠβ͕҉໧తʹఆٛ͞ΕΔ enum TrafficSignal: String { case green case

    yellow case red // ࣦഊՄೳΠχγϟϥΠβ͕҉໧తʹఆٛ͞ΕΔ } let signal = TrafficSignal(rawValue: "yellow")
  35. ΫϥεͷΠχγϟϥΠβ

  36. ܧঝͷಛ௃Λ࣋ͭͨΊɺ֤Ϋϥε֊૚Ͱఆٛ͞ΕͨϓϩύςΟ ͕ॳظԽ͞ΕΔ͜ͱΛอূ͢Δඞཁ͕͋Δ

  37. ͦͷͨΊɺ֤ΠχγϟϥΠβͷϧʔϧ͕͍Ζ͍Ζ 4 ࢦఆΠχγϟϥΠβ 4 ؆қΠχγϟϥΠβ

  38. ࢦఆΠχγϟϥΠβʛDesignated Initializer 4 ඞਢͷΠχγϟϥΠβ 4 શͯͷετΞυϓϩύςΟΛॳظԽ͢Δ 4 ܧঝ͍ͯ͠Δ৔߹͸ɺॳظԽޙʹεʔύʔΫϥεͷࢦఆΠ χγϟϥΠβΛݺͿ

  39. class Book { let title: String let price: Int //

    ࢦఆΠχγϟϥΠβ init(title: String, price: Int) { self.title = title self.price = price } } class Ebook: Book { var urlString: String // ࢦఆΠχγϟϥΠβ init(title: String, price: Int, urlString: String) { self.urlString = urlString super.init(title: title, price: price) } }
  40. ؆қΠχγϟϥΠβʛConvenience Initializer 4 ඞਢͰͳ͍ʢॳظԽॲཧΛศརʹ͢ΔͨΊͷΠχγϟϥΠ βʣ 4 ࢦఆΠχγϟϥΠβΛϥοϓ͠ɺ಺෦ͰݺͿ 4 ಉҰΫϥεͷΠχγϟϥΠβΛݺͼɺ࠷ऴతʹࢦఆΠχγ ϟϥΠβΛݺΜͰ΋OK

    4 ࠷ऴతʹࢦఆΠχγϟϥΠβͰॳظԽ͢Δඞཁ͕͋Δͨ Ίɺఆ਺ϓϩύςΟ͸؆қΠχγϟϥΠβͰॳظԽͰ͖ͳ
  41. class Book { let title: String let price: Int //

    ࢦఆΠχγϟϥΠβ init(title: String, price: Int) { self.title = title self.price = price } } class Ebook: Book { var urlString: String // ࢦఆΠχγϟϥΠβ init(title: String, price: Int, urlString: String) { self.urlString = urlString super.init(title: title, price: price) } // ؆қΠχγϟϥΠβ convenience init(title: String, price: Int, url: URL) { self.init(title: title, price: price, urlString: url.absoluteString) } }
  42. Πϝʔδਤ ֤Ϋϥε֊૚ͰॳظԽ͕ߦΘΕΔྲྀΕ

  43. None
  44. ඞਢΠχγϟϥΠβʛRequired Initializer 4 requiredΩʔϫʔυͷ͍ͭͨΠχγϟϥΠβ͕͋Δ৔߹ɺ αϒΫϥεͰͷ࣮૷͕ඞਢͱͳΔ 4 αϒΫϥεଆͰ͸overrideͷهड़͸ෆཁͰrequiredͷΈه ड़͢Δʢࣗ໌ͳͨΊʣ

  45. class MyView: UIView { // ࢦఆΠχγϟϥΠβ override init(frame: CGRect) {

    super.init(frame: frame) } // ؆қΠχγϟϥΠβ convenience init() { self.init(frame: .zero) } // ඞਢΠχγϟϥΠβ required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } }
  46. ϓϩτίϧͰΠχγϟϥΠβΛఆٛͨ͠৔߹ɺͦͷϓϩτίϧ ʹ४ڌ͢ΔΫϥεͰͷΠχγϟϥΠβ࣮૷͸ඞਢΠχγϟϥΠ βͱͳΔ protocol MyProtocol { var stringValue: String {

    get set } // required͸ࢦఆ͍ͯ͠ͳ͍ init(intValue: Int) } class MyClass: MyProtocol { var stringValue: String // required͕ඞཁ required init(intValue: Int) { stringValue = String(intValue) } }
  47. ͋·Γ࢖Θͳ͍͔΋

  48. ·ͱΊ

  49. ར༻Մೳͳܕ छผ આ໌ ڞ௨ Failable Initilizer/ࣦഊՄೳΠχγϟϥΠβ ॳظԽʹࣦഊ͢ΔՄೳੑͷ͋ΔΠχγϟϥ Πβ ڞ௨ Default

    Initializer/طఆΠχγϟϥΠβ ॳظԽ͕ෆཁͳ৔߹ʹ҉໧తʹ࣮૷͞ΕΔ ΠχγϟϥΠβ ߏ଄ମ Memberwise Initilaizer/શ߲໨Πχγϟϥ Πβ ߏ଄ମͰશͯͷετΞυϓϩύςΟΛॳظԽ ͢ΔΑ͏҉໧తʹ࣮૷͞ΕΔΠχγϟϥΠ β Ϋϥε Designated Initializer/ࢦఆΠχγϟϥΠ β શͯͷετΞυϓϩύςΟΛॳظԽ͠ɺ਌Ϋ ϥε͕͋Ε͹਌ΫϥεͷࢦఆΠχγϟϥΠβ Λ࣮ߦ͢ΔɺඞਢͷΠχγϟϥΠβ Ϋϥε Convenience Initializer/؆қΠχγϟϥΠ β ࢦఆΠχγϟϥΠβΛϥοϓ͢Δɺ೚ҙͷΠ χγϟϥΠβ ྻڍମ Required Initializer/ඞਢΠχγϟϥΠβ αϒΫϥεͰͷΠχγϟϥΠβͷ࣮૷Λඞਢ ͱ͢ΔΠχγϟϥΠβ
  50. ࢀߟ 4 Swift࣮ફೖ໳ʢhttp://gihyo.jp/book/ 2017/978-4-7741-8730-3ʣ 4 ৄղSwiftʢhttp://www.sbcr.jp/products/ 4797390537.htmlʣ 4 ͠Ίḉ೔هʛSwiftͷϓϩύςΟʔͱॳظԽͱ Convenience

    initializer
  51. ͓ΘΓ ͋Γ͕ͱ͏͍͟͝·ͨ͠!!!