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

Mobile Programing Property & Method

Mobile Programing Property & Method

Mobile Programming の授業で使うスライドです

yuichiro_takahashi

November 12, 2018
Tweet

More Decks by yuichiro_takahashi

Other Decks in Technology

Transcript

  1. ςΩετ 2. CLASSͷཁૉ ▸ Classͷதʹ͸༷ʑͳཁૉ͕ଘࡏ͠·͢ class Dog: Animal { let

    name: String private let kind: String init(name: String, kind: String) { self.name = name self.kind = kind } func selfIntroduction() { print("My name is \(name)") print("I'm \(kind)") print("nice to meet you sir.") } } ܧঝ Property ΞΫηεम০ࢠ initializer(constructor) Method
  2. ςΩετ ͜ͷ΁Μͷཁૉʹ͍͓ͭͯ࿩͠͠·͢ ▸ Classͷதʹ͸༷ʑͳཁૉ͕ଘࡏ͠·͢ class Dog: Animal { let name:

    String private let kind: String init(name: String, kind: String) { self.name = name self.kind = kind } func selfIntroduction() { print("My name is \(name)") print("I'm \(kind)") print("nice to meet you sir.") } } Property ΞΫηεम০ࢠ Method
  3. ςΩετ ϓϩύςΟ(PROPERTY)ͱ͸ʁ ▸ ؆୯ʹ͍͏ͱΫϥεͷதʹఆٛ͞Ε͍ͯΔม਺ͷ͜ͱ ▸ ͦͷΫϥε͕ͲΜͳσʔλΛ͔࣋ͭΛද͢ ▸ ྫ͑͹UILabel ͸ text

    ͱ͍͏໊લͷϓϩύςΟΛ͍࣋ͬͯΔ ▸ Labelʹදࣔ͢ΔจࣈྻͷσʔλͷͨΊͷϓϩύςΟ ▸ Propertyʹ͸༷ʑͳछྨ͕͋Δ
  4. ςΩετ ࣮ࡍͷίʔυ class Pokemon { // Property let name: String

    let type1: String let type2: String? var ability: [String] // Initializer(Constructor) init(name: String, type1: String, type2: String?, ability: [String]) { self.name = name self.type1 = type1 self.type2 = type2 self.ability = ability } // Method func doAbility(name: String) { print("\(self.name)͸\(name)Λ࢖ͬͨʂ") print("͔͠͠Կ΋ى͜Βͳ͍ʂ") } } ͜ͷ෦෼͕Property
  5. ςΩετ STORED PROPERTYͱ͸ʁ ▸ ҰൠతͳϓϩύςΟ ▸ ೔ຊޠͰ͸֨ೲܕϓϩύςΟ ▸ σʔλΛΠχγϟϥΠβ΍ΠϯελϯεԽͨ͠ޙʹ୅ೖͯ͠ ࢖͏

    ▸ جຊతͳ࢖͍ํ͸ී௨ͷม਺, ఆ਺ͱมΘΒͳ͍ ▸ ఆ਺͸ initializer ͰͷΈ஋ΛଋറՄೳ
  6. ςΩετ ࣮ࡍͷίʔυ class Pokemon { // Property let name: String

    let type1: String let type2: String? var ability: [String] } let slowpoke = Pokemon(name: "Ϡυϯ", type1: "Έͣ", type2: "Τεύʔ", ability: ["ͶΜΓ͖", "ΈͣͰͬΆ͏", "ͳ·͚Δ", "Ͷͬͱ͏"] ) // var Ͱఆٛ͞Ε͍ͯΔͷͰ୅ೖՄೳ slowpoke.ability = ["ͳ·͚Δ", "ྫྷౚϏʔϜ", "ϋΠυϩϙϯϓ", “Ͳ๨Ε”] // let Ͱఆٛ͞Ε͍ͯΔͷͰ͜Ε͸Ͱ͖ͳ͍ slowpoke.name = “Ϡυϥϯ" ͜Ε͸શ෦ Stored Property
  7. ςΩετ COMPUTED PROPERTYͱ͸ʁ ▸ ͜Ε͸গʑಛघͳϓϩύςΟ ▸ ೔ຊޠͰ͸ܭࢉܕϓϩύςΟͱݺ͹ΕΔ ▸ ඞͣ var

    Ͱఆٛ͞ΕΔ ▸ จ๏͸ var propertyName: Type { return value } ▸ ֨ೲܕͷϓϩύςΟͱ͸ҧ͍ϓϩύςΟʹΞΫηε͢Δͨͼʹϒ ϩοΫͷதͷॲཧ͕࣮ߦ͞Εͯ஋͕ฦ͞ΕΔ ▸ ࣌ؒͷ͔͔Δॲཧ͸ೖΕͪΌμϝ
  8. ςΩετ ࣮ࡍͷίʔυ class Pokemon { // Property let name: String

    let type1: String let type2: String? var ability: [String] } let slowpoke = Pokemon(name: "Ϡυϯ", type1: "Έͣ", type2: "Τεύʔ", ability: ["ͶΜΓ͖", "ΈͣͰͬΆ͏", "ͳ·͚Δ", "Ͷͬͱ͏"] ) let type1 = slowpoke.type1 if let type2 = slowpoke.type2 { print("λΠϓ: \(type1)/\(type2)") } else { print("λΠϓ: \(type1)") } ͜Ε͸શ෦ Stored Property
  9. ςΩετ ࣮ࡍͷίʔυ class Pokemon { // Property let name: String

    let type1: String let type2: String? var ability: [String] // Computed Property var typeText: String { if let t2 = type2 { return "λΠϓ: \(type1)/\(t2)" } return "λΠϓ: \(type1)" } } let slowpoke = Pokemon(name: "Ϡυϯ", type1: "Έͣ", type2: "Τεύʔ", ability: ["ͶΜΓ͖", "ΈͣͰͬΆ͏", "ͳ·͚Δ", "Ͷͬͱ͏"] ) print(slowpoke.typeText) ͜Ε͕ Computed Property
  10. ςΩετ ࣮ࡍͷίʔυ class Pokemon { // Property let name: String

    let type1: String let type2: String? var ability: [String] // Computed Property var typeText: String { if let t2 = type2 { return "λΠϓ: \(type1)/\(t2)" } return "λΠϓ: \(type1)" } } let slowpoke = Pokemon(name: "Ϡυϯ", type1: "Έͣ", type2: "Τεύʔ", ability: ["ͶΜΓ͖", "ΈͣͰͬΆ͏", "ͳ·͚Δ", "Ͷͬͱ͏"] ) print(slowpoke.typeText) ͜Ε͕ Computed Property ΞΫηεͷ࢓ํ͸ Stored Propertyͱಉ͡
  11. ςΩετ LAZY PROPERTYͱ͸ʁ ▸ ͜Ε΋গʑಛघͳϓϩύςΟ ▸ ೔ຊޠͰ͸஗ԆܕϓϩύςΟͱݺ͹ΕΔ ▸ ͪ͜Β΋ඞͣ var

    Ͱఆٛ͞ΕΔ ▸ จ๏͸ lazy var propertyName: Type { return value }() ▸ ѻ͍ํ͸΄ͱΜͲ stored property ͱಉ͕ͩ͡ ▸ ࠷ॳʹΞΫηε͞ΕΔ·ͰॳظԽ͕͞Εͳ͍ ▸ ͪ͜Β͸ٯʹ࣌ؒͷ͔͔ΔॲཧΛೖΕΔͨΊʹ࢖͏ ▸ ViewControllerͰΑ͘࢖͍·͢
  12. ςΩετ ࣮ࡍͷίʔυ class Pokemon { // Property let name: String

    let type1: String let type2: String? var ability: [String] lazy var skill: String = { return "͍͔͘" }() } let slowpoke = Pokemon(name: "Ϡυϯ", type1: "Έͣ", type2: "Τεύʔ", ability: ["ͶΜΓ͖", "ΈͣͰͬΆ͏", "ͳ·͚Δ", "Ͷͬͱ͏"] ) print(slowpoke.skill) ͜Ε͕ Lazy Property ΞΫηεͷ࢓ํ͸ Stored Propertyͱಉ͕ͩ͡ ݺ͹ΕΔ·ͰॳظԽ͞Εͳ͍
  13. ςΩετ PROPERTY OBSERVINGͱ͸ʁ ▸ ͪ͜Β͸ϓϩύςΟͷछྨͰ͸ͳ͘ϓϩύςΟͷػೳ ▸ Observe͸؂ࢹͱ͍͏ҙຯ ▸ ϓϩύςΟͷมߋΛݕ஌ͯ͠ॲཧΛهड़͢Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳΔػೳ ▸

    ϓϩύςΟͷมߋ௚લʹݺ͹ΕΔ willSet { … } ▸ ϓϩύςΟͷมߋ௚ޙʹݺ͹ΕΔ didSet { … } ▸ ͷ2छྨ ▸ { … } ʹॲཧΛॻ͖ࠐΜͰ͍͘
  14. ςΩετ ࣮ࡍͷίʔυ class Pokemon { // Stored Property let name:

    String // লུɾɾɾ // Initializer(Constructor) init(name: String, type1: String, type2: String?, ability: [String]) { // লུɾɾɾ } // Method func doAbility(name: String) { print("\(self.name)͸\(name)Λ࢖ͬͨʂ") print("͔͠͠Կ΋ى͜Βͳ͍ʂ") } func getAbility(abilityName: String) { } func forgetAbility(abilityName: String) { } } // ϝιουݺͼग़͠ slowpoke.doAbility(name: “ͳ·͚Δ") ͜Ε͕ϝιουఆٛ ͜Ε͕ϝιουݺͼग़͠
  15. ςΩετ ΞΫηεम০ࢠʁ ▸ ӳޠͰ͸ Access Control ▸ ม਺΍ఆ਺ɺΫϥεɺϝιουͳͲʹର͢ΔΞΫηεͷ੍ݶΛ؅ཧͰ͖Δػೳ ▸ ͜ΕʹΑΓΫϥεͷ֎͔Β͸৮ͬͯཉ͘͠ͳ͍ϓϩύςΟ΍ϝιουΛӅ͢͜ͱ͕Ͱ͖

    ΔΑ͏ʹͳͬͨΓ ▸ ٯʹϞδϡʔϧͷ֎ʹ·Ͱެ։͢Δ͜ͱ΋Ͱ͖ΔΑ͏ʹͳΔ(ผϓϩδΣΫτ͔Β΋ݟ͑ ΔΑ͏ʹͳΔ) ▸ SwiftͰ͸ΏΔ͍ॱʹ open, public, internal, fileprivate, private ͷޒ͕ͭଘࡏ͢Δ ▸ Ϋϥε΍ϓϩύςΟɺϝιουͷલʹ͚ͭͯ࢖͏ ▸ σϑΥϧτͰ͸ internal
  16. ςΩετ ࣮ࡍͷίʔυ open class Pokemon { // Stored Property let

    name: String // লུɾɾɾ // Initializer(Constructor) init(name: String, type1: String, type2: String?, ability: [String]) { // লུɾɾɾ } // Method private func doAbility(name: String) { print("\(self.name)͸\(name)Λ࢖ͬͨʂ") print("͔͠͠Կ΋ى͜Βͳ͍ʂ") } func getAbility(abilityName: String) { } func forgetAbility(abilityName: String) { } } // Error! slowpoke.doAbility(name: “ͳ·͚Δ") ݺͼग़ͤͳ͘ͳΔ ͜ͷϝιουΛϓϥΠϕʔτʹ Ϋϥε͸Φʔϓϯʹ(ผϓϩδΣΫτ͔Β΋ݟ͑Δ) Կ΋ఆٛ͠ͳ͍৔߹͸ internal ѻ͍ʹͳΔ