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

Observation のあれこれ / A brief introduction about ...

Observation のあれこれ / A brief introduction about Observation

Elvis Shi

June 30, 2023
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. } var employedBy = "YUMEMI Inc." var job = "iOS

    Tech Lead" var favoriteLanguage = "Swift" var twitter = "@lovee" var qiita = "lovee" var github = "el-hoshino" var additionalInfo = """ ৯தಟɺ෠Εͳ͍ʂ """ final class Me: Developable, Talkable {
  2. import Combine class ValuesManager: ObservableObject { @Published var values: [Int]

    = [] } struct ContentView: View { @StateObject private var manager = ValuesManager() var body: some View { List { ForEach(manager.values, id: \.self) { value in Text("\(value)") } Button { manager.values .append(Int.random(in: 0..<100)) } label: { Text("Add") } } .animation(.default, value: manager.values) } }
  3. import Observation @Observable class ValuesManager { var values: [Int] =

    [] } struct ContentView: View { @State private var manager = ValuesManager() var body: some View { List { ForEach(manager.values, id: \.self) { value in Text("\(value)") } Button { manager.values .append(Int.random(in: 0..<100)) } label: { Text("Add") } } .animation(.default, value: manager.values) } } !0CTFSWBCMFΛ σʔλϞσϧ࣮૷ʹ௥Ճ͢Δ σʔλϞσϧΛ ௨ৗͷσʔλͷΑ͏ʹ ϓϩύςΟʔએݴ͢Δ
  4. struct ContentView: View { @State private var manager = ValuesManager()

    var body: some View { Child(manager: manager) } } struct Child: View { var manager: ValuesManager var body: some View { List { ForEach(manager.values, id: \.self) { value in Text("\(value)") } Button { manager.values.append(Int.random(in: 0..<100)) } label: { Text("Add") } } .animation(.default, value: manager.values) } } ࣗ਎ͷॴ༗͡Όͳ͚Ε͹!4UBUF΍ !0CTFSWFE0CKFDU΋ཁΒͳ͍
  5. struct ContentView: View { @State private var manager = ValuesManager()

    var body: some View { List { ForEach($manager.values, id: \.self) { value in Cell(value: value) } Button { manager.values.append(Int.random(in: 0..<100)) } label: { Text("Add") } } .animation(.bouncy, value: manager.values) } } struct Cell: View { @Binding var value: Int var body: some View { Button { value += 1 } label: { Text("\(value)") } } } #JOEJOH͕ඞཁͳͱ͖͸ αΠϯΛೖΕΕ͹Ͱ͖Δ
  6. 0CTFSWBUJPOͱ$PNCJOFͱ$PODVSSFODZͷؔ ܎ 0CTFSWBUJPO $PNCJOF $PODVSSFODZ ࠓ೥ϦϦʔε ೥ϦϦʔε ೥ϦϦʔε J04Ҏ߱ J04Ҏ߱

    J04Ҏ߱ Φϒβʔόʔ ύλʔϯʹઐ೦ ϦΞΫςΟϒ෩ॲཧΛ ఏڙ ඇಉظॲཧͷจ๏Λ ఏڙ
  7. $PNCJOF͸΋͏ඞཁͳ͍ w $PNCJOF͸ͦ΋ͦ΋ΦʔόʔΩϧ w 4XJGU6*͸4UBUF%SJWFOͳͷͰɺ0CTFSWFS1BUUFSO࣮͑͞ݱ Ͱ͖Ε͹े෼Ͱɺ3FBDUJWFʢ'31ʣ͸ඞਢͰ͸ͳ͍ w ඇಉظॲཧΛॻ͖͍͚ͨͩͳΒɺ$PODVSSFODZͷػೳͰे෼ͩ͠ Ή͠Ζॻ͖΍͍͢ w

    Ξοϓϧެࣜ΋$PNCJOF͔Β0CTFSWBUJPO΁ͷҠߦΛਪ঑͍͘͞ w ࠓ೥$PNCJOFͷηογϣϯ͸ԿҰͭͳ͍ w ϥΠϒϥϦʔͷมߋ΋ಛʹͳ͍ w 4XJGU6*ͷυΩϡϝϯτͰ$PNCJOFͷ0CTFSWBCMF0CKFDU͔Β 0CTFSWBUJPOͷ!0CTFSWBCMF΁ͷҠߦهࣄ͕௥Ճ J04Ҏ߱ͷΈରԠͷ৔߹ ͋͘·Ͱචऀͷߟ͑Ͱ͢
  8. $PNCJOF͸΋͏ඞཁͳ͍ w $PNCJOF͸ͦ΋ͦ΋ΦʔόʔΩϧ w 4XJGU6*͸4UBUF%SJWFOͳͷͰɺ0CTFSWFS1BUUFSO࣮͑͞ݱ Ͱ͖Ε͹े෼Ͱɺ3FBDUJWFʢ'31ʣ͸ඞਢͰ͸ͳ͍ w ඇಉظॲཧΛॻ͖͍͚ͨͩͳΒɺ$PODVSSFODZͷػೳͰे෼ͩ͠ Ή͠Ζॻ͖΍͍͢ w

    Ξοϓϧެࣜ΋$PNCJOF͔Β0CTFSWBUJPO΁ͷҠߦΛਪ঑͍͘͞ w ࠓ೥$PNCJOFͷηογϣϯ͸ԿҰͭͳ͍ w ϥΠϒϥϦʔͷมߋ΋ಛʹͳ͍ w 4XJGU6*ͷυΩϡϝϯτͰ$PNCJOFͷ0CTFSWBCMF0CKFDU͔Β 0CTFSWBUJPOͷ!0CTFSWBCMF΁ͷҠߦهࣄ͕௥Ճ J04Ҏ߱ͷΈରԠͷ৔߹ ͋͘·Ͱචऀͷߟ͑Ͱ͢
  9. $PNCJOF͸΋͏ඞཁͳ͍ w $PNCJOF͸ͦ΋ͦ΋ΦʔόʔΩϧ w 4XJGU6*͸4UBUF%SJWFOͳͷͰɺ0CTFSWFS1BUUFSO࣮͑͞ݱ Ͱ͖Ε͹े෼Ͱɺ3FBDUJWFʢ'31ʣ͸ඞਢͰ͸ͳ͍ w ඇಉظॲཧΛॻ͖͍͚ͨͩͳΒɺ$PODVSSFODZͷػೳͰे෼ͩ͠ Ή͠Ζॻ͖΍͍͢ w

    Ξοϓϧެࣜ΋$PNCJOF͔Β0CTFSWBUJPO΁ͷҠߦΛਪ঑͍͘͞ w ࠓ೥$PNCJOFͷηογϣϯ͸ԿҰͭͳ͍ w ϥΠϒϥϦʔͷมߋ΋ಛʹͳ͍ w 4XJGU6*ͷυΩϡϝϯτͰ$PNCJOFͷ0CTFSWBCMF0CKFDU͔Β 0CTFSWBUJPOͷ!0CTFSWBCMF΁ͷҠߦهࣄ͕௥Ճ J04Ҏ߱ͷΈରԠͷ৔߹ ͋͘·Ͱචऀͷߟ͑Ͱ͢
  10. $PNCJOF͸΋͏ඞཁͳ͍ w $PNCJOF͸ͦ΋ͦ΋ΦʔόʔΩϧ w 4XJGU6*͸4UBUF%SJWFOͳͷͰɺ0CTFSWFS1BUUFSO࣮͑͞ݱ Ͱ͖Ε͹े෼Ͱɺ3FBDUJWFʢ'31ʣ͸ඞਢͰ͸ͳ͍ w ඇಉظॲཧΛॻ͖͍͚ͨͩͳΒɺ$PODVSSFODZͷػೳͰे෼ͩ͠ Ή͠Ζॻ͖΍͍͢ w

    Ξοϓϧެࣜ΋$PNCJOF͔Β0CTFSWBUJPO΁ͷҠߦΛਪ঑͍͘͞ w ࠓ೥$PNCJOFͷηογϣϯ͸ԿҰͭͳ͍ w ϥΠϒϥϦʔͷมߋ΋ಛʹͳ͍ w 4XJGU6*ͷυΩϡϝϯτͰ$PNCJOFͷ0CTFSWBCMF0CKFDU͔Β 0CTFSWBUJPOͷ!0CTFSWBCMF΁ͷҠߦهࣄ͕௥Ճ J04Ҏ߱ͷΈରԠͷ৔߹ ͋͘·Ͱචऀͷߟ͑Ͱ͢