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. ϓϩύςΟͱϝιου
    MOBILE PROGRAMMING 11TH NOV

    View Slide

  2. લճ·Ͱͷ͋
    Β͢͡

    View Slide

  3. ςΩετ
    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

    View Slide

  4. ࠓճ͸ɾɾɾ

    View Slide

  5. ςΩετ
    ͜ͷ΁Μͷཁૉʹ͍͓ͭͯ࿩͠͠·͢
    ▸ 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

    View Slide

  6. 1. PROPERTY

    View Slide

  7. ςΩετ
    ϓϩύςΟ(PROPERTY)ͱ͸ʁ
    ▸ ؆୯ʹ͍͏ͱΫϥεͷதʹఆٛ͞Ε͍ͯΔม਺ͷ͜ͱ
    ▸ ͦͷΫϥε͕ͲΜͳσʔλΛ͔࣋ͭΛද͢
    ▸ ྫ͑͹UILabel ͸ text ͱ͍͏໊લͷϓϩύςΟΛ͍࣋ͬͯΔ
    ▸ Labelʹදࣔ͢ΔจࣈྻͷσʔλͷͨΊͷϓϩύςΟ
    ▸ Propertyʹ͸༷ʑͳछྨ͕͋Δ

    View Slide

  8. ςΩετ
    ࣮ࡍͷίʔυ
    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

    View Slide

  9. 1-1. STORED
    PROPERTY

    View Slide

  10. ςΩετ
    STORED PROPERTYͱ͸ʁ
    ▸ ҰൠతͳϓϩύςΟ
    ▸ ೔ຊޠͰ͸֨ೲܕϓϩύςΟ
    ▸ σʔλΛΠχγϟϥΠβ΍ΠϯελϯεԽͨ͠ޙʹ୅ೖͯ͠
    ࢖͏
    ▸ جຊతͳ࢖͍ํ͸ී௨ͷม਺, ఆ਺ͱมΘΒͳ͍
    ▸ ఆ਺͸ initializer ͰͷΈ஋ΛଋറՄೳ

    View Slide

  11. ςΩετ
    ࣮ࡍͷίʔυ
    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

    View Slide

  12. 1-2. COMPUTED
    PROPERTY

    View Slide

  13. ςΩετ
    COMPUTED PROPERTYͱ͸ʁ
    ▸ ͜Ε͸গʑಛघͳϓϩύςΟ
    ▸ ೔ຊޠͰ͸ܭࢉܕϓϩύςΟͱݺ͹ΕΔ
    ▸ ඞͣ var Ͱఆٛ͞ΕΔ
    ▸ จ๏͸ var propertyName: Type { return value }
    ▸ ֨ೲܕͷϓϩύςΟͱ͸ҧ͍ϓϩύςΟʹΞΫηε͢Δͨͼʹϒ
    ϩοΫͷதͷॲཧ͕࣮ߦ͞Εͯ஋͕ฦ͞ΕΔ
    ▸ ࣌ؒͷ͔͔Δॲཧ͸ೖΕͪΌμϝ

    View Slide

  14. ͲΜͳ࣌ʹ࢖
    ͏͔ʁ

    View Slide

  15. ϙέϞϯͷྫ
    Λߟ͑·͢

    View Slide

  16. ςΩετ
    ϙέϞϯͷྫ
    ▸ ϙέϞϯਤؑΛ࡞Δ৔߹Λߟ͑·͢
    ▸ ϠυϯͷΑ͏ʹೋͭͷλΠϓΛ࣋ͭ
    ϙέϞϯ͸
    ▸ λΠϓ1/λΠϓ2ͱ͍͏ܗͰදࣔ͞
    Ε·͢
    ▸ ͜ͷจࣈΛදࣔ͢Δࡍʹߦ͏ॲཧ͸
    ࣍ͷΑ͏ʹͳΔ͸ͣ

    View Slide

  17. ςΩετ
    ࣮ࡍͷίʔυ
    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

    View Slide

  18. COMPUTED PROPERTY
    Λ࢖͏ͱ

    View Slide

  19. ςΩετ
    ࣮ࡍͷίʔυ
    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

    View Slide

  20. ςΩετ
    ࣮ࡍͷίʔυ
    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ͱಉ͡

    View Slide

  21. 1-3. LAZY
    PROPERTY

    View Slide

  22. ςΩετ
    LAZY PROPERTYͱ͸ʁ
    ▸ ͜Ε΋গʑಛघͳϓϩύςΟ
    ▸ ೔ຊޠͰ͸஗ԆܕϓϩύςΟͱݺ͹ΕΔ
    ▸ ͪ͜Β΋ඞͣ var Ͱఆٛ͞ΕΔ
    ▸ จ๏͸ lazy var propertyName: Type { return value }()
    ▸ ѻ͍ํ͸΄ͱΜͲ stored property ͱಉ͕ͩ͡
    ▸ ࠷ॳʹΞΫηε͞ΕΔ·ͰॳظԽ͕͞Εͳ͍
    ▸ ͪ͜Β͸ٯʹ࣌ؒͷ͔͔ΔॲཧΛೖΕΔͨΊʹ࢖͏
    ▸ ViewControllerͰΑ͘࢖͍·͢

    View Slide

  23. ςΩετ
    ࣮ࡍͷίʔυ
    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ͱಉ͕ͩ͡
    ݺ͹ΕΔ·ͰॳظԽ͞Εͳ͍

    View Slide

  24. 2 PROPERTY
    OBSERVING

    View Slide

  25. ςΩετ
    PROPERTY OBSERVINGͱ͸ʁ
    ▸ ͪ͜Β͸ϓϩύςΟͷछྨͰ͸ͳ͘ϓϩύςΟͷػೳ
    ▸ Observe͸؂ࢹͱ͍͏ҙຯ
    ▸ ϓϩύςΟͷมߋΛݕ஌ͯ͠ॲཧΛهड़͢Δ͜ͱ͕Ͱ͖ΔΑ͏ʹͳΔػೳ
    ▸ ϓϩύςΟͷมߋ௚લʹݺ͹ΕΔ willSet { … }
    ▸ ϓϩύςΟͷมߋ௚ޙʹݺ͹ΕΔ didSet { … }
    ▸ ͷ2छྨ
    ▸ { … } ʹॲཧΛॻ͖ࠐΜͰ͍͘

    View Slide

  26. 2. ϝιου

    View Slide

  27. ςΩετ
    ϝιουʁ
    ▸ Ϋϥε΍ߏ଄ମͳͲʹఆٛ͞Ε͍ͯΔؔ਺ͷ͜ͱ
    ▸ ओʹΠϯελϯε͕อ͍࣋ͯ͠ΔσʔλΛՃ޻ͨ͠Γมߋ͠
    ͨΓ͢Δ
    ▸ ϝιου͔Β͸ಉ͡Ϋϥεʹఆٛ͞Ε͍ͯΔଞͷϝιου΍
    ϓϩύςΟʹΞΫηε͢Δ͜ͱ͕Ͱ͖Δ

    View Slide

  28. ςΩετ
    ࣮ࡍͷίʔυ
    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: “ͳ·͚Δ")
    ͜Ε͕ϝιουఆٛ
    ͜Ε͕ϝιουݺͼग़͠

    View Slide

  29. 3. ΞΫηεम
    ০ࢠ

    View Slide

  30. ςΩετ
    ΞΫηεम০ࢠʁ
    ▸ ӳޠͰ͸ Access Control
    ▸ ม਺΍ఆ਺ɺΫϥεɺϝιουͳͲʹର͢ΔΞΫηεͷ੍ݶΛ؅ཧͰ͖Δػೳ
    ▸ ͜ΕʹΑΓΫϥεͷ֎͔Β͸৮ͬͯཉ͘͠ͳ͍ϓϩύςΟ΍ϝιουΛӅ͢͜ͱ͕Ͱ͖
    ΔΑ͏ʹͳͬͨΓ
    ▸ ٯʹϞδϡʔϧͷ֎ʹ·Ͱެ։͢Δ͜ͱ΋Ͱ͖ΔΑ͏ʹͳΔ(ผϓϩδΣΫτ͔Β΋ݟ͑
    ΔΑ͏ʹͳΔ)
    ▸ SwiftͰ͸ΏΔ͍ॱʹ open, public, internal, fileprivate, private ͷޒ͕ͭଘࡏ͢Δ
    ▸ Ϋϥε΍ϓϩύςΟɺϝιουͷલʹ͚ͭͯ࢖͏
    ▸ σϑΥϧτͰ͸ internal

    View Slide

  31. ςΩετ
    ҧ͍·ͱΊ
    ग़య: https://qiita.com/GATA/items/90341f56a56ff584e756

    View Slide

  32. ςΩετ
    ࣮ࡍͷίʔυ
    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 ѻ͍ʹͳΔ

    View Slide