Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

TCAでViewStoreにKeyPath DynamicMemberLookupが使われてる件

yimajo
November 22, 2020

TCAでViewStoreにKeyPath DynamicMemberLookupが使われてる件

iOSアプリ開発のためのFunctional Architecture情報共有会2 の発表資料です。
https://connpass.com/event/194239/

yimajo

November 22, 2020
Tweet

More Decks by yimajo

Other Decks in Programming

Transcript

  1. ͍΍͍΍͍΍ɺ͍͍Ͱ͠ΐɻ Swift 4͘Β͍͔Βͷ KeyPath ɺ ͦͯ͠ Swift 4.2 ͔Βͷ Dynamic

    Member Lookupɺ ͜ΕΛ্ख͍͜ͱ૊Έ߹Θͤͨ Swift 5.1 ͔Βͷ Key Path ʹΑΔ Dynamic Member Lookup ΍Μɺ ͜ΕΛ࢖ͬͯΔͱؒ઀తʹͰ΋StateʹܕͰΞΫηε Ͱ͖ͯؾ࣋ͪ͑ʔΜ΍ɺͦΕҎ্ͷԿ͕͋Μͷ΍ʔʁ
  2. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ • ҙຯ͋Δͷʁ௕ॴ͸͋Δͷʁඞવੑ͸ʁ
  3. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ • ҙຯ͋Δͷʁ௕ॴ͸͋Δͷʁඞવੑ͸ʁ • ͜Ε͸ SwiftUI.View ͚ͩݟͯΔͱҙຯ΍ඞવੑ͸෼͔ΓͮΒ͍͔΋
  4. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ • ҙຯ͋Δͷʁ௕ॴ͸͋Δͷʁඞવੑ͸ʁ • ͜Ε͸ SwiftUI.View ͚ͩݟͯΔͱҙຯ΍ඞવੑ͸෼͔ΓͮΒ͍͔΋ • TCAͰͳ͍ઃܭ΋ҰԠৼΓฦΔ
  5. ࠓճͷ࿩ͷ֓ཁ • TCAͰ͸KeyPathʹΑΔDynamic Member LookupʢҎ߱ KeyPath Member Lookupʣ ͕࢖ΘΕ͍ͯΔ •

    SwiftUI.View ͔Β State ͷ஋ΛݟΔͱ͖ͳͲ • ҙຯ͋Δͷʁ௕ॴ͸͋Δͷʁඞવੑ͸ʁ • ͜Ε͸ SwiftUI.View ͚ͩݟͯΔͱҙຯ΍ඞવੑ͸෼͔ΓͮΒ͍͔΋ • TCAͰͳ͍ઃܭ΋ҰԠৼΓฦΔ • UIKit ͔Β࢖͏৔߹Λ஌ͬͯΔͱඞવੑ͕͋ΔΑ͏ʹࢥ͑Δ
  6. → self.viewStore.publisher.count .assign(to: \.text, on: countLabel) .store(in: &self.cancellables) ࠨͷΑ͏ʹࣗલͰ .publisher

    ʹ .map Λॻ͔ͳ͍͍ͯ͘ʂ KeyPath Member Lookup ࢖ͬͯলུͰ͖Δʢӈʣɻ ͭ·Γ …
  7. 7JFX4UPSF 4UBUF viewStore.count ͍ͩ͘͞ state.count: Int ฦ͢ viewStore.publisher.count ͍ͩ͘͞ StorePublisher<State>͔Β

    StorePublisher<Int>ͳ count ฦ͢ ϓϩύςΟͷ ஋͕ཉ͍͠ 1VCMJTIFSΛ TJOLͯ͠ Ͳ͏ʹ͔͍ͨ͠
  8. ͓ΘΓʹ: ٞ࿦ͷϙΠϯτ • KeyPath Member Lookup ͳͯ͘΋ྑ͍ͱ͸ࢥ͏͚Ͳ΋ • Publisherͱͯ͠ཉ͍͠৔߹ͷΠϯλϑΣʔεͰ͸৑௕͞Λল͚ͯΔ •

    StateͷϓϩύςΟ͕΄͍͠ͱ͖΋ɺKeyPath Member LookupΛ ࢖ͬͨ΄͏͕ରশੑ΍౷Ұײ͕͋Δͷ͸෼͔Δ