Upgrade to Pro — share decks privately, control downloads, hide ads and more …

20171128_iOSLT_Swiftとイニシャライザ

shtnkgm
November 28, 2017

 20171128_iOSLT_Swiftとイニシャライザ

shtnkgm

November 28, 2017
Tweet

More Decks by shtnkgm

Other Decks in Technology

Transcript

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

    4 Designated Initializer/ࢦఆΠχγϟϥΠβ 4 Convenience Initializer/؆қΠχγϟϥΠβ 4 Required Initializer/ඞਢΠχγϟϥΠβ
  2. ΠχγϟϥΠβͷه๏ class MyClass { let a: Int // func͕ෆཁʢinitΩʔϫʔυͷΈʣ init(a:

    Int) { self.a = a } } let case1 = MyClass.init(a: 0) // ݺͼग़࣌͠ͷϝιου໊͕লུՄೳ let case2 = MyClass(a: 0)
  3. ίϯύΠϧΤϥʔͷྫ OptionalͰͳ͍ͷʹname͸࣮࣭nilʢܕͷෆ੔߹ʣ class Book { let name: String // ॳظԽ͞Ε͍ͯͳ͍

    init(name: String) { // ίϯύΠϧΤϥʔ // return from initializer without initializing all stored properties } }
  4. 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() { // ͲͷϓϩύςΟΛॳظԽ͢Δ??? } }
  5. 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 } }
  6. // ʢෆཁʣίϯϐϡʔςουϓϩύςΟ 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?
  7. ॳظԽཁ൱ ϓϩύςΟछผ ิ଍આ໌ ඞཁ σϑΥϧτ஋ͳ͠ɺఆ਺ετΞυ ϓϩύςΟ - ඞཁ σϑΥϧτ஋ͳ͠ɺඇΦϓγϣφ ϧͷม਺ετΞυϓϩύςΟ

    - - Optionalܕͷม਺ετΞυϓϩ ύςΟ ҉໧తʹnilͰॳظԽ͞ΕΔ - σϑΥϧτ஋͋ΓͷετΞυϓϩ ύςΟ σϑΥϧτ஋ͰॳظԽ͞ΕΔ - ίϯϐϡʔςουϓϩύςΟ ஋Λอ࣋͠ͳ͍ͷͰॳظԽ͕ෆ ཁ
  8. 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 } }
  9. class MyClass { var name: String = "MyClass" var number:

    Int = 0 // ҉໧తʹinit()͕࣮૷͞ΕΔ } let myClass = MyClass()
  10. struct Book { let id: String let title: String var

    price: Int // ΠχγϟϥΠβ͸࣮૷͍ͯ͠ͳ͍ } let book = Book(id: "001", title: "SwiftyBook", price: 1500)
  11. 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()
  12. raw value͕ఆٛ͞Ε͍ͯΔ৔߹ ࣦഊՄೳΠχγϟϥΠβ͕҉໧తʹఆٛ͞ΕΔ enum TrafficSignal: String { case green case

    yellow case red // ࣦഊՄೳΠχγϟϥΠβ͕҉໧తʹఆٛ͞ΕΔ } let signal = TrafficSignal(rawValue: "yellow")
  13. 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) } }
  14. 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) } }
  15. 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) } }
  16. ϓϩτίϧͰΠχγϟϥΠβΛఆٛͨ͠৔߹ɺͦͷϓϩτίϧ ʹ४ڌ͢ΔΫϥεͰͷΠχγϟϥΠβ࣮૷͸ඞਢΠχγϟϥΠ βͱͳΔ 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) } }
  17. ར༻Մೳͳܕ छผ આ໌ ڞ௨ Failable Initilizer/ࣦഊՄೳΠχγϟϥΠβ ॳظԽʹࣦഊ͢ΔՄೳੑͷ͋ΔΠχγϟϥ Πβ ڞ௨ Default

    Initializer/طఆΠχγϟϥΠβ ॳظԽ͕ෆཁͳ৔߹ʹ҉໧తʹ࣮૷͞ΕΔ ΠχγϟϥΠβ ߏ଄ମ Memberwise Initilaizer/શ߲໨Πχγϟϥ Πβ ߏ଄ମͰશͯͷετΞυϓϩύςΟΛॳظԽ ͢ΔΑ͏҉໧తʹ࣮૷͞ΕΔΠχγϟϥΠ β Ϋϥε Designated Initializer/ࢦఆΠχγϟϥΠ β શͯͷετΞυϓϩύςΟΛॳظԽ͠ɺ਌Ϋ ϥε͕͋Ε͹਌ΫϥεͷࢦఆΠχγϟϥΠβ Λ࣮ߦ͢ΔɺඞਢͷΠχγϟϥΠβ Ϋϥε Convenience Initializer/؆қΠχγϟϥΠ β ࢦఆΠχγϟϥΠβΛϥοϓ͢Δɺ೚ҙͷΠ χγϟϥΠβ ྻڍମ Required Initializer/ඞਢΠχγϟϥΠβ αϒΫϥεͰͷΠχγϟϥΠβͷ࣮૷Λඞਢ ͱ͢ΔΠχγϟϥΠβ