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

(Appleが)やりたい放題 Swift Attributes

(Appleが)やりたい放題 Swift Attributes

札幌iPhoneアプリ開発者勉強協会資料
https://devsap.connpass.com/event/207402/

題目
1) SwiftUIで説明しづらい@定義
2) Swift Attributesを大枠で分けてみる
3) Swift Attributesから見るSwiftの進化
4) まとめ

0fb0b8c6eecbc8e0a3981501f6a6fea6?s=128

notoroid

March 24, 2021
Tweet

Transcript

  1. "QQMF͕ ΍Γ͍ͨ์୊ 4XJGU"UUSJCVUFT

  2. ࣗݾ঺հ

  3. ೳొཁ !OPUPSPJE w J04ΞϓϦ։ൃऀ w ԰߸͍Γ·͢ిࢉاը w !OPUPSPJE w !JSJNBTV

    w ࠷ۙͷಈ޲ w ϒϩά࡮৽͠·ͨ͠
  4. ॆిใࠂ͞Μ #BUUFSFDFJWFS w όοςϦʔϞχλʔ ϢʔςΟϦςΟʔ  w ࠷େ୆ ࣮ػ ϦϞʔτ୆

     w ख࣋ͪͷJ1IPOF͔Βଞʹ͍࣋ͬͯΔ J1IPOF J1BEͳͲͷػثͷόοςϦʔঢ়ଶ֬ ೝՄೳ w ࣗ୐ʹ͋ΔJ1IPOFͷόοςϦʔঢ়ଶ֬ೝՄ ೳ
  5. ॆిใࠂ͞Μ #BUUFSFDFJWFS w "QQMF8BUDIରԠ w $PNQMJDBUJPOTରԠ w ॆి׬ྃ௨஌ w ॆి׬ྃΛ఻͑ͯ͘ΕΔ௨஌Λ௥Ճ

    w ҎԼʹࢦఆ͢Δ͜ͱͰόοςϦʔΛέ Ξ͢Δ͜ͱ͕Մೳ w Կ͔ͱߴ͍J1IPOFΛ௕࣋ͪͤ͞Δͷʹ༗༻
  6. ࿩͢͜ͱ w 4XJGU"UUSJCVUFTͷେ࿮ w 4XJGU6*ͱ4XJGU"UUSJCVUFTͷؔ܎ w "QQMFͷͱΓ͏Δಈ͖Λ༧ଌ͢Δ

  7. ࿩͞ͳ͍͜ͱ w 4XJGU"UUSJCVUFTͷৄࡉ w ιʔείʔυΛ࢖ͬͨղઆ

  8. ୊໨ w 4XJGU6*Ͱઆ໌ͮ͠Β͍!ఆٛ w 4XJGU"UUSJCVUFTΛେ࿮Ͱ෼͚ͯΈΔ w 4XJGU"UUSJCVUFT͔ΒݟΔ4XJGUͷਐԽ w ·ͱΊ

  9. 4XJGU6*Ͱઆ໌ͮ͠Β͍!ఆٛ

  10. J045FDI#MPH JSJNBTVDPN ઈࢍ౤ߘதͰ͢

  11. 4XJGU6* J04ΞϓϦΛओʹ౤ߘ

  12. ϒϩάهࣄΛ ݕ౼͍ͯͨ͋͠Δ೔

  13. TextFieldͷهࣄΛॻ͜͏

  14. import SwiftUI struct ContentView: View { @State var myfield =

    "" @ScaledMetric(relativeTo: .body) var @ScaledMetric(relativeTo: .body) var @ScaledMetric(relativeTo: .body) var @ScaledMetric(relativeTo: .body) var var body: some View { VStack { Spacer() TextField("Label", text: $my .padding(EdgeInsets(top: .background( Rectangle() .stroke(Color.gr ) .padding() TextField("Label", text: $my .padding(EdgeInsets(top:
  15. import SwiftUI struct ContentView: View { @State var myfield =

    "" @ScaledMetric(relativeTo: .body) var @ScaledMetric(relativeTo: .body) var @ScaledMetric(relativeTo: .body) var @ScaledMetric(relativeTo: .body) var var body: some View { VStack { Spacer() TextField("Label", text: $my .padding(EdgeInsets(top: .background( Rectangle() .stroke(Color.gr ) .padding() TextField("Label", text: $my .padding(EdgeInsets(top:
  16. !4UBUF !4DBMF.FUSJD ͱ͸Կ

  17. !ఆٛʹ͍ͭͯ આ໌ͨ͜͠ͱ͕ͳ͍ɻ

  18. ͦ΋ͦ΋!ఆٛͱ͍͏ ໊শͰ͢Βͳ͍ɻ

  19. !ఆٛͱ͸ͳΜͳͷ͔

  20. QSPQFSUZ8SBQQFS ωλόϨ ͩΑɻ

  21. ͦΜͳࣄ͸ Θ͔͍ͬͯΔͷͩΑɻ

  22. !ఆٛपΓେ࿮Ͱௐ΂ͯ "QQMFͷ܏޲Λ஌Γ͍ͨɻ

  23. 4XJGU"UUSJCVUFΛେ࿮Ͱ ·ͱΊ͍ͯΔ

  24. !ఆٛͷਖ਼໊ࣜশ͸ 4XJGU"UUSJCVUFT

  25. 4XJGU"UUSJCVUFࢀߟαΠτ w 4XJGUͷBUUSJCVUFશղઆʛશछ2JJUB w IUUQTRJJUBDPNTIUOLHNJUFNTDCBCDECD w "UUSJCVUFTŠ5IF4XJGU1SPHSBNNJOH-BOHVBHF 4XJGU  w

    IUUQTEPDTTXJGUPSHTXJGUCPPL3FGFSFODF.BOVBM "UUSJCVUFTIUNM
  26. 4XJGU"UUSJCVUFTᶃ 4XJGUʹ͸ɺએݴʹద༻͞ΕΔଐੑͱܕʹద ༻͞ΕΔଐੑͷछྨ͕͋Γ·͢ɻଐੑ͸ɺ એݴ·ͨ͸ܕʹؔ͢Δ௥Ճ৘ใΛఏڙ͠· ͢ɻ %FFQ-຋༁ ɻ

  27. 4XJGU"UUSJCVUFTᶄ • @attribute name • @attribute name(attribute arguments)

  28. 4XJGU"UUSJCVUFTᶅ J044%,࣌఺Ͱछྨ

  29. BWBJMBCMF JOMJOBCMF PCKD 6*"QQMJDBUJPO.BJO VOLOPXO EJTDBSEBCMF3FTVMU NBJO PCKD.FNCFST VTBCMF'SPN*OMJOF EZOBNJD$BMMBCMF

    OPOPCKD QSPQFSUZ8SBQQFS XBSO@VORVBMJ fi FE@BD DFTT EZOBNJD.FNCFS-P PLVQ /4"QQMJDBUJPO.BJO SFTVMU#VJMEFS BVUPDMPTVSF GSP[FO /4$PQZJOH SFRVJSFT@TUPSFE@QSP QFSUZ@JOJUT DPOWFOUJPO (,*OTQFDUBCMF /4.BOBHFE UFTUBCMF FTDBQJOH
  30. ެࣜͷϖʔδͰ͸ΞϧϑΝϕοτॱ ಛʹάϧʔϓԽ͞Ε͍ͯͳ͍

  31. ଟ͍ͷͰେ࿮Ͱ෼ྨͯ͠Έͨ

  32. େ࿮Ͱ෼ྨᶃ B ઌୡݴޠͱͷޓ׵ੑ C 4XJGUݴޠͷ࢓༷Λิ଍͢Δ΋ͷ D ϑϨʔϜϫʔΫ։ൃͷαϙʔτ E 4UPSZCPBSETʹؔ͢Δಛघࣄ৘ʹΑΔ΋ͷ F

    ݻ༗ϑϨʔϜϫʔΫ΁ͷରԠ G ֎తͳ࢓༷มߋʹରԠ͢Δඞཁ͕͋Δ΋ͷ
  33. େ࿮Ͱ෼ྨᶄ H Ϗϧυ࣌ͷνϡʔχϯάΛ໌ࣔ͢Δ΋ͷ I ςετ࣌ͷརศੑΛਤΔ΋ͷ J ஗ԆධՁɾಈతࢀরΛಋೖ͍ͨ͠΋ͷ K ଞݴޠͱͷݺͼग़͠ํ๏Λॊೈʹ͢Δ΋ͷ L

    4XJGUݴޠࣗମΛ֦ு͢Δ΋ͷ
  34. Bઌୡݴޠͱͷޓ׵ੑ w $ݴޠΛݺͼग़͠Մೳʹ 0CKFDUJF$Λݺͼग़͠Մೳʹ w ؔ਺ϙΠϯλͷޓ׵ੑࢦఆ !DPOWFOUJPO  w ໭Γ஋͕͋Δ͕ෆཁѻ͍ʹରԠ͢ΔͨΊ

    !EJTDBSEBCMF3FTVMU  w 0CKFDUJWF$ͷϓϩύςΟͷίϐʔଐੑରԠ !/4$PQZJOH  w NBJOؔ਺ͷӅṭ !6*"QQMJDBUJPO.BJO !/4"QQMJDBUJPO.BJO  w ϚϧνϓϥοτϑΥʔϜʹ΋ରԠͨ͠NBJOؔ਺ͷӅṭ !NBJO
  35. C4XJGUݴޠͷ࢓༷Λิ଍͢Δ΋ͷ w είʔϓ֎Ͱ΋Ϋϩʔδϟʔอ࣋Λ໌ࣔ͢Δ΋ͷ !FTDBQJOH

  36. DϑϨʔϜϫʔΫ։ൃͷαϙʔτ w ϑϨʔϜϫʔΫར༻ऀʹඇਪ঑Ͱ͋Δ͜ͱΛࣔ͢(@available) w SwiftͰ࡞੒͞ΕͨϑϨʔϜϫʔΫ w ʙΛObjectie-C͔Βݺͼग़͠Մೳʹ(@objc) w ʙΛObjective-C͔Βݺͼग़͠ෆՄʹ(@nonobjc) w

    ʙΛObjective-C͔ΒҰׅͰݺͼग़͠Մೳʹ(@objcMembers) w τοϓϨϕϧͷΦϒδΣΫτ͕ଞϑϨʔϜϫʔΫͱࠞࡏΛආ͚Δ(@warn_unquali fi ed_access) w ໌ࣔతʹকདྷόʔδϣϯͰͷվมΛ੍ݶ͢Δ΋ͷ(@frozen)
  37. E4UPSZCPBSETʹؔ͢Δಛघࣄ৘ʹΑΔ΋ͷ w 4XJGUݴޠͷݫີ͔͞Βݴ͏ͱ*OUFSGBDF#VJMEFSͷ"DUJPO 0VUMFU͸͔ͳΓ ಛघͳಈ͖ w @IBAction w @IBOutlet w

    @IBDesignable w @IBInspectable w @IBSegueAction
  38. Fݻ༗ϑϨʔϜϫʔΫ΁ͷରԠ w (BNF,JUʹରԠ͢Δ΋ͷ !(,*OTQFDUBCMF  w $PSF%BUBʹରԠ͢Δ΋ͷ !/4.BOBHFE  w

    !SFRVJSFT@TUPSFE@QSPQFSUZ@JOJUT
  39. G֎తͳ࢓༷มߋʹରԠ͢Δඞཁ͕͋Δ΋ͷ w ྻڍʹະ஌ͷఆؚ͕ٛ·Εͨ৔߹ͷ৚݅هड़ !VOLOPXO

  40. HϏϧυ࣌ͷνϡʔχϯάΛ໌ࣔ͢Δ΋ͷ w ΠϯϥΠϯల։ʹؔ͢Δ΋ͷ(@inlinable) w ΠϯϥΠϯల։಺Ͱͷར༻ʹ੍ݶ͢Δ΋ͷ(@usableFromInline)

  41. Iςετ࣌ͷརศੑΛਤΔ΋ͷ w ςετ࣌ʹΞΫηεՄೳͱ͢Δ(@testable)

  42. J஗ԆධՁΛಋೖ͍ͨ͠΋ͷ w Ҿ਺Ͱ౉͞Εͨ஋ΛΫϩʔδϟʔԽ͠஗ԆධՁͤ͞Δ(@autoclosure)

  43. Kଞݴޠͱͷݺͼग़͠ํ๏Λॊೈʹ͢Δ΋ͷ w ߏ଄ମΛॊೈͳҾ਺Ͱݺͼग़ͤΔ༷ʹ͢Δ΋ͷ(@dynamicCallable) w ߏ଄ମΛ࣮૷࣌ʹଘࡏ͠ͳ͍ϓϩύςΟ΁ΞΫηεՄೳͱ͢Δ΋ͷ (@dynamicMemberLookup)

  44. LݴޠࣗମΛ֦ு͢Δ΋ͷ w ม਺ʹ෇ՃͰ͖Δ4XJGU"UUSJCVUFΛ֦ு͢Δ !QSPQFSUZ8SBQQFS w ؔ਺ͷ໭Γ஋Λ༧໿ݴޠʹ·Ͱհೖ͠ػೳ֦ு͢Δ΋ͷ(@resultBuilder)

  45. 4XJGU"UUSJCVUFTͷҹ৅͸ ࡶଟͳػೳͷू߹ʹݟ͑Δ

  46. ࡶଟ͕ͩ਺Λҙࣝ͠ͳ͍ͷ͸ ༻్͝ͱ8FCͰݕࡧͯ͠࢖͏

  47. 4XJGU"UUSJCVUFʹݴޠࣗମΛ֦ு͢Δ ػೳ͕૿͑ͨ͜ͱͰݕࡧΩʔϫʔυΛ બͿͷʹ೉ّ

  48. 4XJGU"UUSJCVUFT͔ΒݟΔ 4XJGUͷਐԽ

  49. छʹ෼͚͕ͨ

  50. छͰ΋ଟ͍Θ

  51. छ͔ΒछΛϐοΫΞοϓ

  52. 4XJGUਐԽͷྺ࢙ᶃ w 4XJGUॳظ w !*#0VUMFU !*#"DUJPO w $ݴޠɺ0CKFDUJWF$ͷػೳݺͼग़͠ɺ4%,ରԠ

  53. Bઌୡݴޠͱͷޓ׵ੑ w $ݴޠΛݺͼग़͠Մೳʹ 0CKFDUJF$Λݺͼग़͠Մೳʹ w ؔ਺ϙΠϯλͷޓ׵ੑࢦఆ !DPOWFOUJPO  w ໭Γ஋͕͋Δ͕ෆཁѻ͍ʹରԠ͢ΔͨΊ

    !EJTDBSEBCMF3FTVMU  w 0CKFDUJWF$ͷϓϩύςΟͷίϐʔଐੑରԠ !/4$PQZJOH  w NBJOؔ਺ͷӅṭ !6*"QQMJDBUJPO.BJO !/4"QQMJDBUJPO.BJO  w ϚϧνϓϥοτϑΥʔϜʹ΋ରԠͨ͠NBJOؔ਺ͷӅṭ !NBJO
  54. E4UPSZCPBSETʹؔ͢Δಛघࣄ৘ʹΑΔ΋ͷ w 4XJGUݴޠͷݫີ͔͞Βݴ͏ͱ*OUFSGBDF#VJMEFSͷ"DUJPO 0VUMFU͸͔ͳΓ ಛघͳಈ͖ w @IBAction w @IBOutlet w

    @IBDesignable w @IBInspectable w @IBSegueAction
  55. 4XJGUਐԽͷྺ࢙ᶄ w 4XJGU࢓༷҆ఆظ w 4XJGUͷ࢓༷͕ݻ·Γ࢝Ί֦ுػೳ͕4XJGU"UUSJCVUF΁௥Ճ͢ΔΑ͏ʹ w 4XJGU͚ͩͰϑϨʔϜϫʔΫ࣮૷Մೳʹ

  56. DϑϨʔϜϫʔΫ։ൃͷαϙʔτ w ϑϨʔϜϫʔΫར༻ऀʹඇਪ঑Ͱ͋Δ͜ͱΛࣔ͢(@available) w SwiftͰ࡞੒͞ΕͨϑϨʔϜϫʔΫ w ʙΛObjectie-C͔Βݺͼग़͠Մೳʹ(@objc) w ʙΛObjective-C͔Βݺͼग़͠ෆՄʹ(@nonobjc) w

    ʙΛObjective-C͔ΒҰׅͰݺͼग़͠Մೳʹ(@objcMembers) w τοϓϨϕϧͷΦϒδΣΫτ͕ଞϑϨʔϜϫʔΫͱࠞࡏΛආ͚Δ(@warn_unquali fi ed_access) w ໌ࣔతʹকདྷόʔδϣϯͰͷվมΛ੍ݶ͢Δ΋ͷ(@frozen)
  57. 4XJGUਐԽͷྺ࢙ᶅ w ࠷ۙ ʙ  w ଞݴޠͰͷݺͼग़͠Λ༻ҙʹ͢ΔͨΊಈతࢀরΛ࠶औΓࠐΈ w !ఆٛΛ֦ுͰ͖ΔΑ͏ʹ w

    ໭Γ஋ͷ֦ு ༧໿ޠ·Ͱؔ༩͢Δڍಈมߋ͕Մೳ
  58. 4XJGUͷΞοϓσʔτྺ࢙ᶅ w ࠷ۙ ʙ  w ଞݴޠͰͷݺͼग़͠Λ༻ҙʹ͢ΔͨΊಈతࢀরΛ࠶औΓࠐΈ w !ఆٛΛ֦ுͰ͖ΔΑ͏ʹ w

    ໭Γ஋ͷ֦ு ༧໿ޠ·Ͱؔ༩͢Δڍಈมߋ͕Մೳ ॏཁ
  59. LݴޠࣗମΛ֦ு͢Δ΋ͷ w ม਺ʹ෇ՃͰ͖Δ4XJGU"UUSJCVUFΛ֦ு͢Δ !QSPQFSUZ8SBQQFS w ؔ਺ͷ໭Γ஋Λ༧໿ݴޠʹ·Ͱհೖ͠ػೳ֦ு͢Δ΋ͷ(@resultBuilder)

  60. 4XJGU6*Ͱͷ4XJGU"UUSJCVUF w !SFTVMU#VJMEFS !@GVODUJPO#VJMEFS͔Β໊শมߋ  w 4XJGUͷ༧໿ޠɺฒͼ·Ͱؔ༩ 4XJGU6*্ͰͷJGจ  w

    هड़ϧʔϧΛڧ੍ 4XJGU6*Ͱ͸ݴޠͷ%4-Խʹ࢖༻  w هड़ΤϥʔΛίϯύΠϥϨϕϧͰิ଍Ͱ͖Δ w !QSPQFSUZ8SBQQFS w ม਺ʹ෇༩͢Δ4XJGU"UUSJCVUFΛ֦ு ಠࣗఆٛͷ௥Ճ  w 4XJGU6*Ͱ͸ม਺ͷମࡋΛอͪͭͭ୺తʹػೳ෇༩ʹ࢖༻
  61. struct ContentView: View { @State var isRounded = true //

    propertyWrapper // ViewͷBody͸1ͭͷView͔͠هड़Ͱ͖ͳ͍ var body: some View { // isRounded ʙ else ·Ͱ͸resultBuilderʹΑΔهड़ϧʔϧͰ͋ͬͯݴޠͷifจͱ͸ҟͳΔ // ৗʹܕ͸ҧ͏͕1ͭͷViewΛฦ͢ if isRounded { RoundedRectangle(cornerRadius: 20) .foregroundColor(.orange) .frame(width: 72, height: 72) .onTapGesture { isRounded = !isRounded } } else { Rectangle() .foregroundColor(.blue) .frame(width: 72, height: 72) .onTapGesture { isRounded = !isRounded } } } }
  62. struct ContentView: View { @State var isRounded = true //

    propertyWrapper // ViewͷBody͸1ͭͷView͔͠هड़Ͱ͖ͳ͍ var body: some View { // isRounded ʙ else ·Ͱ͸resultBuilderʹΑΔهड़ϧʔϧͰ͋ͬͯݴޠͷifจͱ͸ҟͳΔ // ৗʹܕ͸ҧ͏͕1ͭͷViewΛฦ͢ if isRounded { RoundedRectangle(cornerRadius: 20) .foregroundColor(.orange) .frame(width: 72, height: 72) .onTapGesture { isRounded = !isRounded } } else { Rectangle() .foregroundColor(.blue) .frame(width: 72, height: 72) .onTapGesture { isRounded = !isRounded } } } } !QSPQFSUZ8SBQQFS !SFTVMU#VJMEFS
  63. ։ൃऀ͔Βݟͨ 4XJGUͷΠϝʔδ ˡ$ݴޠɺ0CKFDUJWF$΋αϙʔτ͢Δ৽ݴޠ ˡݴޠػೳ͕ॆ࣮͠ɺ'31Ͱ࠷৽ϓϩάϥϛϯάٕ ज़ΛಋೖͰ͖ΔΑ͏ʹͳͬͨɻ 4XJGU6* ݴޠػೳ͕ҙਤ͠ͳ͍ํ޲Ͱ֦ு

  64. "QQMF͕΍Γ͍ͨ์୊4XJGU • ݴޠతʹ͸Web։ൃऀʹ਌࿨ੑ͕͋Δ༷ʹઃܭ • FRPʹ઀ۙ͢ΔΘ͚Ͱ΋ͳ͍(ϦΞΫςΟϒϓϩάϥϛϯά) • OOPʹݻࣥ͢ΔΘ͚Ͱ΋ͳ͍(ΦϒδΣΫτࢦ޲) • ݴޠ࢓༷͸AppleҎ֎΋αϙʔτ͢Δ •

    (UbuntuɺCentOSɺAmazon Linux, Windows10) • ݴޠ࢓༷͕ݻ·ͬͯ΋Apple͸૊ΈࠐΈ͍ͨػೳ͸૊ΈࠐΉ
  65. ͔͜͜Β͸༧ଌ

  66. "QQMFͷ΍Γ͍ͨ์୊͸ Ͳ͜ʹ޲͔͏ͷ͔

  67. ৽σόΠε

  68. ΋ͪΖΜͦ͏ͩ

  69. ΋ͬͱ਎ۙͳଘࡏ͕ ͍Δ͡Όͳ͍͔ɻ

  70. 8FC"TTFNCMZ͔Βͷ 8FCϑϩϯτΤϯυ ରԠ

  71. ·ͱΊ w 4XJGU"UUSJCVUFTʹ͍ͭͯେ࿮Ͱ·ͱΊ·ͨ͠ w 4XJGUͷݴޠ࢓༷͔Β֎ΕΔػೳ͕ࡶଟʹ4XJGU"UUSJCVUFTʹ௥Ճ͞ Ε͍ͯ·͢ w 4XJGUͷݴޠ࢓༷ʹؔ܎ͳ͘"QQMF͸ࣗ෼ͷ΍Γ͍ͨػೳΛͶ͡ࠐΈ ·͢

  72. IUUQTJSJNBTVDPN <13>#MPHਵ࣌౤ߘத

  73. ࢀߟ w 4XJGUͷBUUSJCVUFશղઆʛશछ2JJUB w IUUQTRJJUBDPNTIUOLHNJUFNTDCBCDECD w "UUSJCVUFTŠ5IF4XJGU1SPHSBNNJOH-BOHVBHF 4XJGU  w

    IUUQTEPDTTXJGUPSHTXJGUCPPL3FGFSFODF.BOVBM"UUSJCVUFTIUNM w ʲ4XJGUʳ1SPQFSUZ8SBQQFSTͱ͸ʁc଎Ͱา͘ώτ w IUUQTXXXOEXBMLFSDPNTXJGUQSPQFSUZXSBQQFST w 'VODUJPO#VJMEFSͰ༡΅͏2JJUB w IUUQTRJJUBDPNUBSVOPOJUFNTFEBDDCE w 4XJGUͰ3FBDUJWF1SPHSBNNJOH2JJUB w IUUQTRJJUBDPNTIPIFJZPLPZBNBJUFNTBBBCE