Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

SwiftUI の @State, @ObservedObject, @Environment...

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 の説明を加筆しました

kumamotone

November 11, 2019
Tweet

More Decks by kumamotone

Other Decks in Programming

Transcript

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

    ࠷ۙࣾ಺༻ͷΞϓϦΛSwiftUIͰ࡞Γ·ͨ͠ ۽ຊ ࿨ਖ਼ (@kumamo_tone)
  2. 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ܕͷ ϓϩύςΟ
  3. 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ͷঢ়ଶ͕มΘΔͱɺ উखʹมΘΔ
  4. 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͕͍͍ͭͯΕ͹
  5. • @State • @ObservedObject • @EnvironentObject View Binding༻ͷProperty Wrappers Property

    Wrappersʹ͍ͭͯͷ ͘Θ͍͠આ໌͸ɺҎલεϥΠυʹ·ͱΊͨͷͰ Α͔ͬͨΒࢀߟʹ͍ͯͩ͘͠͞
  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) } } ͜ͷϓϩύςΟ ͷมߋ͕ ݕ஌Ͱ͖ΔΑ͏ʹͳΔ
  7. struct ContentView: View { @State var user: User } struct

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

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

    ContentView: View { @State var userFirstName: String @State var userLastName: String }
  10. class User: ObservableObject { @Published var userFirstName: String @Published var

    userLastName: String } ద߹͢Δͱ !0CTFSWFE0CKFDU ͱͯ͠࢖͑Δ
  11. class User: ObservableObject { @Published var userFirstName: String @Published var

    userLastName: String } ద߹͢Δͱ !0CTFSWFE0CKFDU ͱͯ͠࢖͑Δ !1VCMJTIFEΛ͚ͭΔͱ มߋΛݕ஌Ͱ͖ΔΑ͏ʹͳΔ
  12. 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Λ
 ࢦఆ
  13. 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") }}} ΠχγϟϥΠβʹ ౉͢
  14. 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") }}}
  15. 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") }}} Կ΋౉͞ͳͯ͘Α͍
  16. • @State • ϓϩύςΟΛόΠϯσΟϯάͰ͖Δ • @ObservedObject • ࣗ࡞ΫϥεΛόΠϯσΟϯάͰ͖Δ • όΠϯσΟϯά͢ΔPropertyʹ@PublishedΛ͚ͭΔ

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