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

20180220_iOSLT_Swiftとオブジェクト間の通知のパターン

shtnkgm
February 20, 2018

 20180220_iOSLT_Swiftとオブジェクト間の通知のパターン

shtnkgm

February 20, 2018
Tweet

More Decks by shtnkgm

Other Decks in Programming

Transcript

  1. 5ͭͷ௨஌ύλʔϯ 1. ॥؀ࢀরύλʔϯʢΞϯνύλʔϯʣ 2. Delegateύλʔϯʢऑࢀর+ϙϦϞʔϑΟζϜʣ 3. NotificationCenterύλʔϯ 4. KVOύλʔϯʢKey-Value Observing:

    Ωʔ஋؂ࢹʣ 5. Closure Callbackύλʔϯ ※SwiftͰඪ४αϙʔτ͞Ε͍ͯͳ͍ɺRxSwift΍BondͳͲͷσʔλόΠϯσΟϯάύλʔϯ͸ର৅֎
  2. class View { // ˒ ModelΦϒδΣΫτ΁ͷࢀর var model: Model =

    Model() func receiveUIEvent() { // ModelͷviewϓϩύςΟʹࣗ਎Ληοτ model.view = self // 2. σʔλͷߋ৽ΛModelʹ௨஌ model.updateData() } // 5. UIͷߋ৽ func updateUI() { print("\(model.data)") } }
  3. class Model { // ˒ ViewΦϒδΣΫτ΁ͷࢀর var view: View? var

    data: Int = 0 // 3. σʔλͷߋ৽ func updateData() { data += 1 // 4. View΁σʔλͷߋ৽׬ྃΛ௨஌ view?.updateUI() } }
  4. ϓϩτίϧ͕ܕͱͯ͠ѻ͑Δ • ΠϯελϯεԽ͸Ͱ͖ͳ͍͕ɺܕͱͯ͠ѻ͑Δ protocol ViewProtocol: class { func updateUI() }

    class Model { // ViewͰͳ͘ViewProtocolʹґଘ weak var view: ViewProtocol? var data: Int = 0 func updateData() { data += 1 view?.updateUI() } }
  5. ۩৅ΫϥεΛϓϩτίϧʹ४ڌͤ͞Δ // ViewProtocolʹ४ڌ͢Δ͜ͱΛએݴ class View: ViewProtocol { var model: Model

    = Model() func receiveUIEvent() { model.view = self model.updateData() } // ViewProtocolʹ४ڌͨ͠ϝιου func updateUI() { print("\(model.data)") } }
  6. ิ଍: Class-Only Protocols protocol ViewProtocol: class { func updateUI() }

    ্هͷίʔυྫͰ͸ɺViewProtocol͸classΛܧঝ͍ͤͯ͞·͢ɻ classΛܧঝ͢Δͱɺͦͷϓϩτίϧ͸Ϋϥεʹ͔͠ద༻Ͱ͖ͳ͘ ͳΓ·͢ɻ
  7. Model͔ΒView΁௨஌͢Δྫ class View { var model: Model = Model() init()

    { // ௨஌ͷड৴ొ࿥ʢupdateDataNotificationͷ௨஌Λड৴ͨ͠ΒɺupdateUIϝιουΛ࣮ߦʣ NotificationCenter.default.addObserver(self, selector: #selector(updateUI), name: .updateDataNotification, object: nil) } func receiveUIEvent() { model.updateData() } @objc func updateUI() { print("\(model.data)") } }
  8. class Model { var data: Int = 0 func updateData()

    { data += 1 // ௨஌ͷૹ৴ʢupdateDataNotificationͷ௨஌Λૹ৴ʣ NotificationCenter.default.post(name: .updateDataNotification, object: nil) } }
  9. class View { var model: Model = Model() // ؂ࢹ༻ΦϒδΣΫτΛอ࣋͢Δ

    var observation: NSKeyValueObservation? init() { // modelͷdataϓϩύςΟΛKVOͰ؂ࢹ͢Δ observation = model.observe(\.data, options: [.new]) { model, change in // model.data͕มԽͨ͠৔߹ʹ࣮ߦ͞ΕΔΫϩʔδϟʔ if let newValue = change.newValue { print(newValue) } } } func receiveUIEvent() { model.updateData() } }
  10. Ϋϩʔδϟʔ͸ؔ਺ͷΦϒδΣΫτ // Ϋϩʔδϟʔͷྫ let isEqual: (_ a: Int, _ b:

    Int) -> Bool = { a, b in return a == b } // ()Ͱ࣮ߦ isEqual(1, 1) // true isEqual(1, 2) // false
  11. class View { var model: Model = Model() func receiveUIEvent()

    { // ׬ྃޙͷॲཧΛΫϩʔδϟʔͰࢦఆ͢Δ // ຤ඌͷΫϩʔδϟʔͷҾ਺໊͸লུͰ͖Δʢtrailing closureه๏ʣ model.updateData { data in print(data) } } }
  12. class Model { var data: Int = 0 // ׬ྃޙͷॲཧΛҾ਺ͷΫϩʔδϟʔͰड͚औ࣮ͬͯߦ͢Δ

    func updateData(completion: (_ data: Int) -> Void) { data += 1 completion(data) } }
  13. ௨஌ͷύλʔϯ ௨஌ݩͱ௨஌ઌͷ਺ ϝϦοτ σϝϦοτ Delegate 1ɿ1 ϓϩτίϧʹΑΓ࣮૷͢΂͖ ௨஌ΠϯλϑΣʔε͕໌֬ɻ ௨஌͢Δϝιου͕1ͭͷ৔ ߹͸ɺهड़ྔʹݟ߹Θͳ

    ͍ɻ Notification NɿN ௚઀ͷࢀর͕ͳ͍ΦϒδΣΫ τؒͰ΋௨஌͕ՄೳɻΦϒδ ΣΫτ͕ؒૄ݁߹ɻ ଟ༻͢Δͱॲཧ͕௥͍ͮΒ ͘ͳΔɻάϩʔόϧͳείʔ ϓͰ௨஌Λߦ͏ͨΊɺϓϩ άϥϚ͕ҙਤ͠ͳ͍ॲཧ͕ ಈ͍ͯ͠·͏Մೳੑ͕͋Δɻ KVO 1ɿN WKWebViewͳͲKVOʹରԠ ͨ͠Ϋϥεͱ૬ੑ͕ྑ͍ɻ Objective-CͷϥϯλΠϜ͕ඞ ཁɻߏ଄ମͰ͸ར༻Ͱ͖ͳ ͍ɻ Closure Callback 1ɿ1 ॲཧͷґཔ෦෼ͱ׬ྃޙͷ ॲཧΛۙ͘ʹॻ͘͜ͱ͕Ͱ ͖ɺՄಡੑ͕ߴ͍ɻ ωετ૿͑͗͢΍callbackϝ ιουଟ͗͢ͰՄಡੑ☓ɻ