SwiftUI の @State, @ObservedObject, @EnvironmentObject

266af7918931b0ea15eae9e8f42ecfa6?s=47 kumamotone
November 11, 2019

SwiftUI の @State, @ObservedObject, @EnvironmentObject

potatotips #66 (iOS/Android開発Tips共有会) - connpass
https://potatotips.connpass.com/event/149806/

11/11 23:07 ObservedObject の説明を加筆しました

266af7918931b0ea15eae9e8f42ecfa6?s=128

kumamotone

November 11, 2019
Tweet

Transcript

  1. 2.

    • iOS/AndroidΤϯδχΞˏϠϑʔ • ษڧձӡӦ • Bonfire iOSɺWWDC Extended ͳͲ •

    ࠷ۙࣾ಺༻ͷΞϓϦΛSwiftUIͰ࡞Γ·ͨ͠ ۽ຊ ࿨ਖ਼ (@kumamo_tone)
  2. 4.
  3. 5.

    struct ContentView: View { @State var isOn: Bool var body:

    some View { Toggle(isOn: $isOn) { if self.isOn { Text("On") } else { Text("Off") } }.frame(width: 200) } } #PPMܕͷ ϓϩύςΟ
  4. 6.

    struct ContentView: View { @State var isOn: Bool var body:

    some View { Toggle(isOn: $isOn) { if self.isOn { Text("On") } else { Text("Off") } }.frame(width: 200) } } #PPMܕͷ ϓϩύςΟ 7JFXΛએݴ #PPMͷঢ়ଶ͕มΘΔͱɺ উखʹมΘΔ
  5. 7.

    struct ContentView: View { @State var isOn: Bool var body:

    some View { Toggle(isOn: $isOn) { if self.isOn { Text("On") } else { Text("Off") } }.frame(width: 200) } } #PPMܕͷ ϓϩύςΟ 7JFXΛએݴ #PPMͷঢ়ଶ͕มΘΔͱɺ উखʹมΘΔ !4UBUF͕͍͍ͭͯΕ͹
  6. 8.

    • @State • @ObservedObject • @EnvironentObject View Binding༻ͷProperty Wrappers Property

    Wrappersʹ͍ͭͯͷ ͘Θ͍͠આ໌͸ɺҎલεϥΠυʹ·ͱΊͨͷͰ Α͔ͬͨΒࢀߟʹ͍ͯͩ͘͠͞
  7. 9.
  8. 11.

    struct ContentView: View { @State var isOn: Bool var body:

    some View { Toggle(isOn: $isOn) { if self.isOn { Text("On") } else { Text("Off") } }.frame(width: 200) } } ͜ͷϓϩύςΟ ͷมߋ͕ ݕ஌Ͱ͖ΔΑ͏ʹͳΔ
  9. 16.

    struct ContentView: View { @State var user: User } struct

    ContentView: View { @State var userFirstName: String @State var userLastName: String } ʢTUSVDUʹͨ͠৔߹ʣ ϓϩύςΟͷมߋ͕͋Δͱɺ શମΛஔ͖׵͑Δඞཁ͕ ͋ΔͨΊɺDMBTTʹ͍ͨ͠ ͱ͖͕͋Δ
  10. 17.

    struct ContentView: View { @State var user: User } struct

    ContentView: View { @State var userFirstName: String @State var userLastName: String } ʢDMBTTʹͨ͠৔߹ʣ VTFSͷมߋ͸ݕ஌Ͱ͖Δ͕ɺ VTFSpSTU/BNF ͷมߋ͸ݕ஌Ͱ͖ͳ͍
  11. 18.

    struct ContentView: View { @ObservedObject var user: User } struct

    ContentView: View { @State var userFirstName: String @State var userLastName: String }
  12. 19.

    class User: ObservableObject { @Published var userFirstName: String @Published var

    userLastName: String } ద߹͢Δͱ !0CTFSWFE0CKFDU ͱͯ͠࢖͑Δ
  13. 20.

    class User: ObservableObject { @Published var userFirstName: String @Published var

    userLastName: String } ద߹͢Δͱ !0CTFSWFE0CKFDU ͱͯ͠࢖͑Δ !1VCMJTIFEΛ͚ͭΔͱ มߋΛݕ஌Ͱ͖ΔΑ͏ʹͳΔ
  14. 23.
  15. 25.

    struct ContentView: View { @ObservedObject var settings: GameSettings = GameSettings()

    var body: some View { NavigationView { List { NavigationLink(destination: NextView(settings: self.settings)) { Text("\(settings.isHard ? "Hard" : "Easy")") }}}}} struct NextView: View { @ObservedObject var settings: GameSettings var body: some View { Toggle(isOn: $settings.isHard) { self.settings.isHard ? Text("Hard") : Text("Easy") }}} ભҠઌͷ7JFXΛ
 ࢦఆ
  16. 26.

    struct ContentView: View { @ObservedObject var settings: GameSettings = GameSettings()

    var body: some View { NavigationView { List { NavigationLink(destination: NextView(settings: self.settings)) { Text("\(settings.isHard ? "Hard" : "Easy")") }}}}} struct NextView: View { @ObservedObject var settings: GameSettings var body: some View { Toggle(isOn: $settings.isHard) { self.settings.isHard ? Text("Hard") : Text("Easy") }}} ΠχγϟϥΠβʹ ౉͢
  17. 29.

    struct ContentView: View { @ObservedObject var settings: GameSettings = GameSettings()

    var body: some View { NavigationView { List { NavigationLink(destination: NextView(settings: self.settings)) { Text("\(settings.isHard ? "Hard" : "Easy")") }}}}} struct NextView: View { @ObservedObject var settings: GameSettings var body: some View { Toggle(isOn: $settings.isHard) { self.settings.isHard ? Text("Hard") : Text("Easy") }}}
  18. 30.

    struct ContentView: View { @EnvironmentObject var settings: GameSettings var body:

    some View { NavigationView { List { NavigationLink(destination: NextView()) { Text("\(settings.isHard ? "Hard" : "Easy")") }}}}} struct NextView: View { @EnvironmentObject var settings: GameSettings var body: some View { Toggle(isOn: $settings.isHard) { self.settings.isHard ? Text("Hard") : Text("Easy") }}} Կ΋౉͞ͳͯ͘Α͍
  19. 32.
  20. 33.

    • @State • ϓϩύςΟΛόΠϯσΟϯάͰ͖Δ • @ObservedObject • ࣗ࡞ΫϥεΛόΠϯσΟϯάͰ͖Δ • όΠϯσΟϯά͢ΔPropertyʹ@PublishedΛ͚ͭΔ

    • @EnvironentObject • ΠχγϟϥΠβܦ༝ͰViewؒͷσʔλͷड͚౉͕͠Ͱ͖Δ • @EnvironentObject Ͱ Environment ܦ༝Ͱ΋ड͚౉͕͠Ͱ͖Δ ·ͱΊ