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

Mobile Programming Initializer Beta

Mobile Programming Initializer Beta

モバイルプログラミングの授業で使うスライドです

yuichiro_takahashi

November 28, 2018
Tweet

More Decks by yuichiro_takahashi

Other Decks in Technology

Transcript

  1. ςΩετ ΠχγϟϥΠβʁ ▸ JavaͰݴ͏ॴͷίϯετϥΫλ ▸ ΫϥεΛॳظԽ͠ѻ͑ΔΑ͏ʹ४උΛߦ͏৔ॴ ▸ ॳظԽ͸ओʹϓϩύςΟʹॳظ஋Ληοτͨ͠ΓඞཁʹԠ͡ ͯϝιου΋ݺͿ ▸

    SwiftͰ͸ඞͣ init() { … } ͱ͍͏໊લʹͳΔ ▸ ΠχγϟϥΠβͰ͸શͯͷϓϩύςΟΛॳظԽ͢Δ·Ͱϝιο υ΍ଞͷϓϩύςΟʹΞΫηεͰ͖ͳ͍
  2. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String var ability:

    Ability var level: Int fileprivate(set) var health: Int = 1 fileprivate var healthFactor: Int { return 1 } fileprivate var baseHealth: Int { return 10 } init(name: String, level: Int, ability: Ability) { self.name = name self.ability = ability self.level = level self.health = self.baseHealth + level * self.healthFactor } } ·ͣ͸஋͕ೖͬͯͳ͍ϓϩύςΟΛॳظԽ શͯͷϓϩύςΟʹ஋͕ೖΔͱbaseHealth ΍ healthFactorʹ ΞΫηεͰ͖Δ
  3. ςΩετ ͳͥશͯͷϓϩύςΟΛೖΕͳ͍ͱ͍͚ͳ͍͔ ▸ Ҏલ࢖ΘΕ͍ͯͨObjective-CͳͲͷݴޠͰ͸ॳظԽ͕͞Ε͍ͯͳ͍৔߹͸nilͳͲ ΛೖΕͯڧҾʹॳظԽΛ͍ͯͨ͠ ▸ ͜ͷڍಈΛڐͯ͠͠·͏ͱ༧ظͤ͵Τϥʔ͕ى͜Δ͜ͱ͕͋Δ(Null Pointer Exceptionͱ͔) ▸

    SwiftͰ͸ͦΜͳ͜ͱ͕ى͜Βͳ͍Α͏ʹίϯύΠϧ࣌ʹม਺΍ఆ਺ʹ஋͕͖ͪΜͱ ೖ͍ͬͯΔ͔νΣοΫ͢ΔΑ͏ʹͳ͍ͬͯΔ ▸ ͜ΕʹΑΓ஋ΛೖΕͯͨͱࢥ͚ͬͯͨͲೖΕͯͳ͔ͬͨʂ ▸ ͱ͍ͬͨώϡʔϚϯΤϥʔΛճආ͢Δ͜ͱ͕Ͱ͖Δ ▸ Swiftͬͯૉఢɾɾɾૉఢ͡Όͳ͍ʁ
  4. ςΩετ ৭ʑͳΠχγϟϥΠβ ▸ Failable Initializer ▸ Default Initializer ▸ Memberwise

    Initializer ▸ Designated Initializer ▸ Convenience Initializer ▸ Required Initializer
  5. ςΩετ ৭ʑͳΠχγϟϥΠβ ▸ Failable Initializer: ࣦഊՄೳΠχγϟϥΠβ ▸ Default Initializer: نఆΠχγϟϥΠβ

    ▸ Memberwise Initializer: શ߲໨ΠχγϟϥΠβ ▸ Designated Initializer: ࢦఆΠχγϟϥΠβ ▸ Convenience Initializer: ؆қΠχγϟϥΠβ ▸ Required Initializer: ඞਢΠχγϟϥΠβ
  6. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String var ability:

    Ability var level: Int fileprivate(set) var health: Int = 1 fileprivate var healthFactor: Int { return 1 } fileprivate var baseHealth: Int { return 10 } init(name: String, level: Int, ability: Ability) { self.name = name self.ability = ability ɹɹɹɹ self.level = level self.health = self.baseHealth + level * self.healthFactor } } ࢦఆΠχγϟϥΠβ
  7. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String = "Pikachu"

    var level: Int = 100 fileprivate(set) var health: Int = 1 fileprivate var healthFactor: Int { return 1 } fileprivate var baseHealth: Int { return 10 } // ҉໧తʹΠχγϟϥΠβ͕ఆٛ͞ΕΔ } let pikachu = Pokemon()
  8. ςΩετ ࣮ࡍͷίʔυ class WebPage { let title: String let url:

    URL init?(title: String, urlString: String) { guard let url = URL(string: urlString) else { return nil } self.title = title self.url = url } } let google: WebPage? = WebPage(title: "google", urlString: "https://www.google.com") Failable Initializer ࣦഊ͢Δ͜ͱ͕͋ΔͷͰ OptionalܕʹͳΔ
  9. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String var ability:

    Ability var level: Int fileprivate(set) var health: Int = 1 fileprivate var healthFactor: Int { return 1 } fileprivate var baseHealth: Int { return 10 } required init(name: String, level: Int, ability: Ability) { self.name = name self.ability = ability ɹɹɹɹ self.level = level self.health = self.baseHealth + level * self.healthFactor } } ඞਢΠχγϟϥΠβ
  10. ςΩετ ࣮ࡍͷίʔυ // ͬͪ͜͸Τϥʔ class Pikachu: Pokemon { init(level: Int)

    { let ability = Ability(name: "ͰΜ͖γϣοΫ", damage: 20) super.init(name: "ϐΧνϡ΢", level: level, ability: ability) } } 
 // ͜Ε͸OK class Pikachu: Pokemon { required override init(name: String, level: Int, ability: Ability) { super.init(name: name, level: level, ability: ability) } }
  11. ςΩετ ؆қΠχγϟϥΠβ (CONVENIENCE INITIALIZER)? ▸ ؆қతͳΠχγϟϥΠβ ▸ جຊతʹࢦఆΠχγϟϥΠβͱηοτͰఆٛ͞ΕΔαϒͷΠ χγϟϥΠβ ▸

    ࢦఆΠχγϟϥΠβͰ͸ϓϩύςΟʹ͢΂ͯ஋Ληοτ͢Δ ඞཁ͕͋Δ ▸ ॳظ஋͕ηοτ͞Ε͍ͯͳ͍ϓϩύςΟ͕ఆٛͯ͋͠Δ৔߹ ͸ඞͣ͜ͷλΠϓͷΠχγϟϥΠβΛఆٛ͠ͳͯ͘͸͍͚ͳ ͍
  12. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String var ability:

    Ability var level: Int = 0 fileprivate(set) var health: Int = 1 init(name: String, level: Int, ability: Ability) { self.name = name self.level = level self.ability = ability self.health = level * 5 } convenience init(level: Int) { let ability = Ability(name: "ѱ͕͖͋", damage: 40) self.init(name: "͚ͭ͹Μ", level: level, ability: ability) } } ؆қΠχγϟϥΠβ
  13. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String var ability:

    Ability var level: Int = 0 fileprivate(set) var health: Int = 1 init(name: String, level: Int, ability: Ability) { self.name = name self.level = level self.ability = ability self.health = level * 5 } convenience init(level: Int) { let ability = Ability(name: "ѱ͕͖͋", damage: 40) self.init(name: "͚ͭ͹Μ", level: level, ability: ability) } } ؆қΠχγϟϥΠβ جຊతʹࢦఆΠχγϟϥΠβͱηοτͰఆٛ͞ΕΔ
  14. ςΩετ ࣮ࡍͷίʔυ class Pokemon { let name: String var ability:

    Ability var level: Int = 0 fileprivate(set) var health: Int = 1 init(name: String, level: Int, ability: Ability) { self.name = name self.level = level self.ability = ability self.health = level * 5 } convenience init(level: Int) { let ability = Ability(name: "ѱ͕͖͋", damage: 40) self.init(name: "͚ͭ͹Μ", level: level, ability: ability) } } ؆қΠχγϟϥΠβ جຊతʹࢦఆΠχγϟϥΠβͱηοτͰఆٛ͞ΕΔ ඞͣଞͷࢦఆΠχγϟϥΠβ͔ ਌ΫϥεͷΠχγϟϥΠβΛݺͼग़͢
  15. ςΩετ શ߲໨ΠχγϟϥΠβ (MEMBERWISE INITIALIZER)? ▸ গʑಛघͳΠχγϟϥΠβ ▸ classʹ͸ଘࡏͤͣ struct ʹͷΈੜ੒͞ΕΔΠχγϟϥΠβ

    ▸ structʹΠχγϟϥΠβΛҰͭ΋ఆٛ͠ͳ͍৔߹ɺఆٛ͞Ε ͍ͯΔϓϩύςΟશͯΛҾ਺ʹؚΜͩΠχγϟϥΠβ͕҉໧ తʹఆٛ͞Ε͍ͯΔ ▸ σʔλΛϞσϦϯά͚ͨͩ͠ͷstructͳͲΛఆٛͨ͠ࡍʹ͢ ͘͝ศར
  16. ςΩετ ࣮ࡍͷίʔυ struct Ability { let name: String let type:

    String let damage: Int let hitRate: Int // ҉໧తʹҎԼͷΑ͏ͳΠχγϟϥΠβ͕ఆٛ͞ΕΔ /* init(name: String, type: String, damage: Int, hitRate: Int) { self.name = name self.type = type self.damage = damage self.hitRate = hitRate } */ } let ability = Ability( name: "ίϝοτύϯν", type: "͸͕Ͷ", damage: 90, hitRate: 90 ) શͯͷϓϩύςΟΛؚΜͩΠχγϟϥΠβ͕࢖͑Δ