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

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

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

Elvis Shi

June 30, 2023
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. 0CTFSWBUJPOͷ͋Ε͜Ε
    f o r ୈ ճ ) " , "5" T X J G U

    View Slide

  2. }
    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 {

    View Slide

  3. 88%$ָ͠Ί·͔ͨ͠ʁ

    View Slide

  4. IUUQTTQFBLFSEFDLDPNNOBSVTFXXEDYJBOEJDBOKJBCBPHBPUJZBOKJCJBO

    View Slide

  5. FUD
    8JEHFU,JU
    4XJGU6*
    0CTFSWBUJPO
    WJTJPO04
    3FBMJUZ$PNQPTFS1SP
    4XJGU%BUB
    4XJGUNBDSPT
    3FBMJUZ,JU
    6*,JU
    "DUJWJUZ,JU
    7JTJPO,JU
    $PSF.PUJPO

    View Slide

  6. FUD
    8JEHFU,JU
    4XJGU6*
    0CTFSWBUJPO
    WJTJPO04
    3FBMJUZ$PNQPTFS1SP
    4XJGU%BUB
    4XJGUNBDSPT
    3FBMJUZ,JU
    6*,JU
    "DUJWJUZ,JU
    7JTJPO,JU
    $PSF.PUJPO

    View Slide

  7. 0CTFSWBUJPO
    w 4XJGU6*༻ͷ৽͍͠ϑϨʔϜϫʔΫ
    w σʔλมߋͷ௥੻͕͞ΒʹָʹͳΔ
    w ύϑΥʔϚϯε΋্͕ΔΒ͍͠

    View Slide

  8. 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)
    }
    }

    View Slide

  9. 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Λ
    σʔλϞσϧ࣮૷ʹ௥Ճ͢Δ
    σʔλϞσϧΛ
    ௨ৗͷσʔλͷΑ͏ʹ
    ϓϩύςΟʔએݴ͢Δ

    View Slide

  10. 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΋ཁΒͳ͍

    View Slide

  11. 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͕ඞཁͳͱ͖͸
    αΠϯΛೖΕΕ͹Ͱ͖Δ

    View Slide

  12. ͋Εʁ
    0CTFSWBUJPOͬͯ͜Μͳʹ؆୯ʁ

    View Slide

  13. View Slide

  14. View Slide

  15. 0CTFSWBUJPOͱ
    $PNCJOFͷؔ܎

    View Slide

  16. 0CTFSWBUJPOͱ$PNCJOFͷؔ܎
    0CTFSWBUJPO $PNCJOF
    ࠓ೥ϦϦʔε ೥ϦϦʔε
    J04Ҏ߱ J04Ҏ߱
    Φϒβʔόʔύλʔϯʹઐ೦ ϦΞΫςΟϒ෩ͷॲཧΛఏڙ

    View Slide

  17. 0CTFSWBUJPOͱ$PNCJOFͱ$PODVSSFODZͷؔ
    ܎
    0CTFSWBUJPO $PNCJOF
    ࠓ೥ϦϦʔε ೥ϦϦʔε
    J04Ҏ߱ J04Ҏ߱
    Φϒβʔόʔ
    ύλʔϯʹઐ೦
    ϦΞΫςΟϒ෩ॲཧΛ
    ఏڙ

    View Slide

  18. 0CTFSWBUJPOͱ$PNCJOFͱ$PODVSSFODZͷؔ
    ܎
    0CTFSWBUJPO $PNCJOF $PODVSSFODZ
    ࠓ೥ϦϦʔε ೥ϦϦʔε ೥ϦϦʔε
    J04Ҏ߱ J04Ҏ߱ J04Ҏ߱
    Φϒβʔόʔ
    ύλʔϯʹઐ೦
    ϦΞΫςΟϒ෩ॲཧΛ
    ఏڙ
    ඇಉظॲཧͷจ๏Λ
    ఏڙ

    View Slide

  19. $PNCJOF͸΋͏ඞཁͳ͍
    J04Ҏ߱ͷΈରԠͷ৔߹
    ͋͘·Ͱචऀͷߟ͑Ͱ͢

    View Slide

  20. $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Ҏ߱ͷΈରԠͷ৔߹
    ͋͘·Ͱචऀͷߟ͑Ͱ͢

    View Slide

  21. $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Ҏ߱ͷΈରԠͷ৔߹
    ͋͘·Ͱචऀͷߟ͑Ͱ͢

    View Slide

  22. $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Ҏ߱ͷΈରԠͷ৔߹
    ͋͘·Ͱචऀͷߟ͑Ͱ͢

    View Slide

  23. $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Ҏ߱ͷΈରԠͷ৔߹
    ͋͘·Ͱචऀͷߟ͑Ͱ͢

    View Slide

  24. 0CTFSWBUJPOʹඋ͑Α

    View Slide

  25. ࢀߟࢿྉ
    w IUUQTEFWFMPQFSBQQMFDPNWJEFPTQMBZXXED
    w IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOPCTFSWBUJPO
    w IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOTXJGUVJNJHSBUJOH
    GSPNUIFPCTFSWBCMFPCKFDUQSPUPDPMUPUIFPCTFSWBCMFNBDSP

    View Slide