Data Essentials in SwiftUI

Data Essentials in SwiftUI

集まれSwift好き!Swift愛好会スピンオフ WWDC20セッション要約会 @オンライン

21e251d97f836f77e7dce83fcaf6daff?s=128

ry-itto

July 09, 2020
Tweet

Transcript

  1. 3ZPZB*UP !SZJUUP  %BUB&TTFOUJBMTJO4XJGU6* ू·Ε4XJGU޷͖ʂ4XJGUѪ޷ձεϐϯΦϑ88%$ηογϣϯཁ໿ձ!ΦϯϥΠϯ 

  2. ࣗݾ঺հ ҏ౻྇໵ !SZJUUP  ձ௡େֶֶ෦೥ੜ Χϐόϥ͕޷͖

  3. ηογϣϯͷ಺༰ w 4XJGU6*Ͱͷσʔλϑϩʔ w ΞϓϦͷσʔλϞσϧઃܭ w ࣮ࡍʹΞϓϦʹऔΓೖΕΔࡍͷςΫχοΫ

  4. ηογϣϯͷ಺༰ w 4XJGU6*Ͱͷσʔλϑϩʔ w ΞϓϦͷσʔλϞσϧઃܭ w ࣮ࡍʹΞϓϦʹऔΓೖΕΔࡍͷςΫχοΫ

  5. 4XJGU6*Ͱͷσʔλϑϩʔ ϏϡʔΛ࡞Δ࣌ʹߟ͍͑ͨ̏ͭͷ࣭໰ w ࡞੒͢ΔϏϡʔͰඞཁͳσʔλ͸Կ͔ʁ w ࡞੒͢ΔϏϡʔͰ͸σʔλΛͲͷΑ͏ʹૢ࡞͢Δ͔ʁ w σʔλ͸Ͳ͔͜ΒདྷΔʢ౉͞ΕΔʣͷ͔ʁ

  6. 4XJGU6*Ͱͷσʔλϑϩʔ ϏϡʔΛ࡞Δ࣌ʹߟ͍͑ͨ̏ͭͷ࣭໰ w ࡞੒͢ΔϏϡʔͰඞཁͳσʔλ͸Կ͔ʁ CPPLͱ͍͏#PPLܕͷ஋ QSPHSFTTͱ͍͏%PVCMFܕͷ஋ ͦΕͧΕ͕͋Ε͹ϏϡʔΛදࣔͰ͖ͦ͏ɻ w ࡞੒͢ΔϏϡʔͰ͸σʔλΛͲͷΑ͏ʹૢ࡞͢Δ ͔ʁ

    ಛʹͳͦ͞͏ w σʔλ͸Ͳ͔͜ΒདྷΔʢ౉͞ΕΔʣͷ͔ʁ ͜ͷϏϡʔΛ࢖͏ϏϡʔͰ౉͞ΕΔ͜ͱʹͳΓͦ͏ɻ struct BookCard : View { let book: Book let progress: Double var body: some View { HStack { Cover(book.coverName) VStack(alignment: .leading) { TitleText(book.title) AuthorText(book.author) } Spacer() RingProgressView(value: progress) } } }
  7. 4XJGU6*Ͱͷσʔλϑϩʔ ϏϡʔΛ࡞Δ࣌ʹߟ͍͑ͨ̏ͭͷ࣭໰ w ࡞੒͢ΔϏϡʔͰඞཁͳσʔλ͸Կ͔ʁ CPPLͱ͍͏#PPLܕͷ஋ QSPHSFTTͱ͍͏%PVCMFܕͷ஋ ͦΕͧΕ͕͋Ε͹ϏϡʔΛදࣔͰ͖ͦ͏ɻ w ࡞੒͢ΔϏϡʔͰ͸σʔλΛͲͷΑ͏ʹૢ࡞͢ Δ͔ʁ

    ಛʹͳͦ͞͏ w σʔλ͸Ͳ͔͜ΒདྷΔʢ౉͞ΕΔʣͷ͔ʁ ͜ͷϏϡʔΛ࢖͏ϏϡʔͰ౉͞ΕΔ͜ͱʹͳΓͦ͏ɻ struct BookCard : View { let book: Book let progress: Double var body: some View { HStack { Cover(book.coverName) VStack(alignment: .leading) { TitleText(book.title) AuthorText(book.author) } Spacer() RingProgressView(value: progress) } } } l4PVSDFPG5SVUIzͱ͍͏ݴ༿Ͱηογϣϯ಺Ͱ͸ѻΘΕΔ
  8. 4XJGU6*Ͱͷσʔλϑϩʔ ϏϡʔͰͷσʔλอ࣋ɺͦΕͧΕͲΜͳ࣌ʹ࢖͏ʁ ϓϩύςΟϏϡʔʹΑͬͯมߋ͞Εͳ͍σʔλ !4UBUFϏϡʔʹΑͬͯอ࣋͞ΕΔҰ࣌తͳσʔλΛѻ͏ !#JOEJOHଞͷϏϡʔʹΑͬͯอ࣋͞Ε͍ͯΔσʔλΛมߋ͍ͨ͠৔߹

  9. ηογϣϯͷ಺༰ w 4XJGU6*Ͱͷσʔλϑϩʔ w ΞϓϦͷσʔλϞσϧઃܭ w ࣮ࡍʹΞϓϦʹऔΓೖΕΔࡍͷςΫχοΫ

  10. ΞϓϦͷσʔλϞσϧઃܭ σʔλϞσϧఆٛ w 0CTFSWBCMF0CKFDUΛ࢖͏ w !1VCMJTIFEΛ࢖͏ w ̎ͭ໨ͷ࣭໰ ࡞੒͢ΔϏϡʔͰ͸σʔλΛͲͷΑ͏ʹૢ࡞͢ Δ͔ʁ

     ʹ౴͑Δͷ͸༰қ class CurrentlyReading: ObservableObject { let book: Book @Published var progress = ReadingProgress() @Published var isFinished = false var currentProgress: Double { isFinished ? 1.0 : progress.progress } }
  11. ΞϓϦͷσʔλϞσϧઃܭ ϏϡʔͰ࡞ͬͨϞσϧΛ࢖͏ w ଞͷ̎ͭͷ࣭໰ʹ͍ͭͯ͸ʁ w ࡞੒͢ΔϏϡʔͰඞཁͳσʔλ͸Կ͔ʁ w ࡞੒͢ΔϏϡʔͰ͸σʔλΛͲͷΑ͏ʹૢ࡞͢Δ͔ʁ w σʔλ͸Ͳ͔͜ΒདྷΔʢ౉͞ΕΔʣͷ͔ʁ

  12. ΞϓϦͷσʔλϞσϧઃܭ ϏϡʔͰ࡞ͬͨϞσϧΛ࢖͏ w ѻ͏ͨΊͷ1SPQFSUZ8SBQQFS͕̏ͭ w !0CTFSWFE0CKFDU w !&OWJSPONFOU0CKFDU w !4UBUF0CKFDU

    /FX
  13. ΞϓϦͷσʔλϞσϧઃܭ ϏϡʔͰ࡞ͬͨϞσϧΛ࢖͏ !0CTFSWFE0CKFDU σʔλͷґଘؔ܎Λ࡞Δ !&OWJSPONFOU0CKFDU Ϗϡʔ֊૚ؒͷ0CTFSWBCMF0CKFDUͷѻ͍Λศརʹ͢Δ !4UBUF0CKFDU /FX  0CTFSWBCMF0CKFDUΛϏϡʔͷϥΠϑαΠΫϧʹ݁ͼ͚ͭΔ

  14. ηογϣϯͷ಺༰ w 4XJGU6*Ͱͷσʔλϑϩʔ w ΞϓϦͷσʔλϞσϧઃܭ w ࣮ࡍʹΞϓϦʹऔΓೖΕΔࡍͷςΫχοΫ

  15. ࣮ࡍʹΞϓϦʹऔΓೖΕΔࡍͷςΫχοΫ ໰୊ w ࢠͷϏϡʔͰϞσϧΛϓϩύςΟͰ ͍࣋ͬͯͯɺͦ͜ͰॳظԽ͍ͯ͠Δ ৔߹ w 3FBEJOH-JTU7JFXFS͕ࡍϨϯμϦϯά͞ ΕΔͨͼʹϞσϧͷΠϯελϯε͕ੜ੒ ͞Εͯ͠·͏

    struct ReadingListViewer: View { var body: some View { NavigationView { ReadingList() Placeholder() } } } struct ReadingList: View { @ObservedObject var store = ReadingListStore() var body: some View { // ... } }
  16. ࣮ࡍʹΞϓϦʹऔΓೖΕΔࡍͷςΫχοΫ ղܾࡦ w !4UBUF0CKFDUΛ୅ΘΓʹ࢖͏͜ͱ Ͱղܾ͕Ͱ͖ΔʢΒ͍͠ʣ w !4UBUF0CKFDUΛ࢖༻͢Δͱ4XJGU6*͕ ద੾ͳλΠϛϯάͰΠϯελϯεԽΛ͠ ͯ͘ΕΔͱͷ͜ͱ struct

    ReadingListViewer: View { var body: some View { NavigationView { ReadingList() Placeholder() } } } struct ReadingList: View { @StateObject var store = ReadingListStore() var body: some View { // ... } }
  17. ࣮ࡍʹΞϓϦʹऔΓೖΕΔࡍͷςΫχοΫ ୭͕σʔλΛ࣋ͭͷ͔ w ڞ௨ͷ૆ઌʹ࣋ͭ w !4UBUF0CKFDUΛ࢖ͬͯͦͷϏϡʔࣗ਎͕࣋ͭ w ΞϓϦ಺ͷάϩʔόϧͳσʔλͱͯ࣋ͭ͠ w "QQΛܧঝͨ͠Ϗϡʔʹ!4UBUFͳͲΛར༻ͯ͠એݴͯ͋͛͠Δ

  18. ࣮ࡍʹΞϓϦʹऔΓೖΕΔࡍͷςΫχοΫ σʔλͷӬଓԽ w 4DFOF4UPSBHF w 4XJGU6*͕؅ཧ͍ͯ͠ΔϏϡʔͰ͔͠࢖͑ͳ͍ w ߲໨ͷબ୒ঢ়ଶͳͲΛอଘ w "QQ4UPSBHF

    w 6TFS%FGBVMUΛ࢖༻ͨ͠άϩʔόϧετϨʔδ w ઃఆͳͲͷখ͞ͳσʔλΛอଘ
  19. ͓·͚ ৽͘͠௥Ճ͞ΕͨΠϕϯτιʔε w PO$IBOHF w PO0QFO63- w PO$POUJOVF6TFS"DUJWJUZ