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

How to migrate to Swift 4

2594ac7ce91fd7d9a3ce71ca7cc2d0c0?s=47 d_date
June 27, 2017

How to migrate to Swift 4

2017/06/27 Geek Garage iOS vol.3

2594ac7ce91fd7d9a3ce71ca7cc2d0c0?s=128

d_date

June 27, 2017
Tweet

Transcript

  1. How to migrate to Swift 4 Daiki Matsudate,@d_date, iOS Freelancer

    Distribution GeekGarage
  2. Daiki Matsudate,@d_date • iOS Freelancer • Supporting Realm as News

    Translator • Organizer of CLEM (announce later) • WWDC17 Attendee
  3. Daiki Matsudate,@d_date • iOS Freelancer • Supporting Realm as News

    Translator • Organizer of CLEM (announce later) • WWDC17 Attendee
  4. Agenda • How to migrate to Swift 4 using Xcode

    9 Beta • [SE-0160] Limited @objc inference • [SE-0110] Distinguish between single-tuple and multiple-argument function types • Renamed some constants • Decodable
  5. [SE-0160] Limited @objc inference • https://github.com/apple/swift-evolution/blob/master/ proposals/0160-objc-inference.md

  6. @objc in Swift 3.x • Objective-CͰݺͼग़ͤΔ΋ͷʹ͍ͭͯɺSwiftଆͰ໌ࣔత ʹએݴͰ͖Δ • Θ͟Θ͟@objcΛॻ͔ͳͯ͘΋͍͍Α͏ʹ ਪ࿦ͯ͘͠

    ΕΔ
  7. @objc in Swift 3.x ͷ໰୊఺ • Objective-CͰݺͼग़ͤΔ΋ͷʹ͍ͭͯɺSwiftଆͰ໌ࣔత ʹએݴͰ͖Δ • Θ͟Θ͟@objcΛॻ͔ͳͯ͘΋͍͍Α͏ʹ

    ਪ࿦ͯ͘͠ ΕΔ → ա৒Ͱ͸ʁʁ
  8. • SwiftͷΫϥε͸Objective-CͷSelectorͷিಥΛҾ ͖ى͕ͪ͜͠ @objc in Swift 3.x ͷ໰୊఺

  9. • Swift API Design GuidelineʹԊͬͯॻ͍ͯ΋ɺ Objective-CͰݺͼग़͢ͱ͖ʹ͸Objective-C Coding Guidelineʹ൓͢Δ໊લʹม׵͞Ε͕ͪɻ ಛʹɺinitializer͸ɺͦͷ໊લʹୈҰҾ਺ΛؚΊͳ ͯ͘͸͍͚ͳ͍ͷͰɺ@objc͸ඞ͚ͣͭΔ͜ͱʹ

    ͳΔɻ @objc in Swift 3.x ͷ໰୊఺
  10. • SwiftίϯύΠϥ͸Objective-C Calling Convention ͔ΒSwift Calling Convention΁ม׵͢Δ"thunk"ϝ ιουΛ࡞ΔͨΊɺόΠφϦαΠζ͕૿େ͢Δɻ @objc in

    Swift 3.x ͷ໰୊఺
  11. @objc in future

  12. @objc in future Objective-CͰඞཁͳͱ͜Ζ͚ͩʹ @objcΛ͚ͭΔΑ͏ʹਪ࿦ΛݮΒ͢ ʲํ਑ʳ

  13. Ҿ͖ଓ͖@objcΛਪ࿦͢Δ΋ͷ 1.@objcͷ͍͍ͭͯΔએݴͷΦʔόʔϥΠυͷͱ͖

  14. Ҿ͖ଓ͖@objcΛਪ࿦͢Δ΋ͷ 2. @objcͷ͍ͭͨϓϩτίϧͷཁ݅Λຬͨ͢ͱ͖

  15. Ҿ͖ଓ͖@objcΛਪ࿦͢Δ΋ͷ 3. @IBOutlet,@IBActionଐੑ͕͍͍ͭͯΔͱ͖ 4. @NSManagedଐੑ͕͍͍ͭͯΔͱ͖ Interface BuilderͱͷΠϯλϥΫγϣϯ͕ Objective-CϥϯλΠϜͰߦΘΕΔͨΊ Core DataͱͷΠϯλϥΫγϣϯ͕

    Objective-CϥϯλΠϜͰߦΘΕΔͨΊ
  16. ௥ՃͰ@objcΛਪ࿦͢Δ΋ͷ 1. @GKInspectableଐੑ͕͍͍ͭͯΔͱ͖ 2. @IBInspectableଐੑ͕͍͍ͭͯΔͱ͖ GameplayKitͱͷΠϯλϥΫγϣϯ͕ Objective-CϥϯλΠϜͰߦΘΕΔͨΊ Interface BuilderͱͷΠϯλϥΫγϣϯ͕ Objective-CϥϯλΠϜͰߦΘΕΔͨΊ

  17. @objcΛਪ࿦͠ͳ͘ͳΔ΋ͷ 1. dynamic

  18. @objcΛਪ࿦͠ͳ͘ͳΔ΋ͷ 2. NSObject༝དྷͷΫϥε NSObjectΛܧঝ͍ͯ͠ΔΫϥεͰ͋ͬͯ΋ɺ @objcͳ͠Ͱ͸Objective-C͔Βݺͼग़ͤͳ͍ɻ

  19. @objcΛਪ࿦͠ͳ͘ͳΔ΋ͷ 2. NSObject༝དྷͷΫϥε extensionͰ͸ɺ@objcͳ͠Ͱ͸Objective-C͔Βݺͼ ग़ͤͳ͍ɻ

  20. @objcMembersͷಋೖ Ϋϥεશମ͕@objcͰ͋Δ͜ͱ͕ٻΊΒΕΔࡍ͸ɺ @objcMembersΛ͚ͭΔ

  21. @objcMembersͷಋೖ @objcMembersΛ͚ͭΔΫϥε͸ɺObjecive-Cଆ Ͱ swift_objc_membersΛ͚ͭΔ

  22. Extensionʹ͓͚Δ@objcͷON/OFF @nonobjcΛextensionʹ͚ͭΔ͜ͱͰɺ@objcͷ ର৅֎ͱ͢Δ͜ͱ͕Ͱ͖Δɻ

  23. Swift 4Ҡߦͷͱ͖͸ Ͳ͏͢Ε͹͍͍ͷ…ʁ

  24. "Minimal" migration workflow!

  25. Minimal migration workflow • Step 1: Swift 4ϞʔυͰɺ@objcਪ࿦͞ΕͨՕॴͷ͢΂ͯͷܯࠂΛղ ফ͢Δɻ

  26. Minimal migration workflow • Step 2: SWIFT_DEBUG_IMPLICIT_OBJC_ENTRYPOINTΛ1ʙ3ͷؒ ͷ஋ʹઃఆ͠ɺΞϓϦΛςετɻ 1.ΤϯτϦϙΠϯτ΁ͷݺग़ΛϝοηʔδͰه࿥ 2.ϩάΛͱΓɺόοΫτϨʔεΛൃߦ

    3.όοΫτϨʔεͰه࿥͠ɺΫϥογϡ͠·͢ɻ
  27. Minimal migration workflow • Step 3: ͜ͷ࣌఺ͰɺSwift 4ʹҠߦՄೳɻSwift 4ΛϏϧυ͢Δͱɺ ഇࢭ༧ఆͷϧʔϧʹج͍ͮͯ@objc͕ਪଌ͞Εͨ࢒Γͷέʔ

    εͷObjective-CΤϯτϦϙΠϯτ͕࡟আ͞ΕΔɻ
  28. Summary in @objc • dynamic, NSObject༝དྷͷΫϥεͰ͸@objcਪ࿦͕͞Ε ͳ͘ͳΔ • @objcMembersʹΑΓclassશମʹ@objcΛదԠͰ͖ΔΑ ͏ʹͳΔ

    • @nonobjcΛextensionʹదԠ͢Δ͜ͱͰͦͷextension͸ @objcͷదԠ֎ͱ͢Δ͜ͱ͕Ͱ͖Δ
  29. [SE-0110] Distinguish between single- tuple and multiple-argument function types

  30. [SE-0110] Distinguish between single-tuple and multiple-argument function types •Swift 3.1

    • Swift 4Ͱมߋ
  31. Renamed some constants

  32. NSAttributedStringKey • Renamed to enum • NSFontAttributedName -> NSAttributedStringKey.font •

    NSParagraphStyleAttributeName -> NSAttributedStringKey.paragraphStyle
  33. NSAttributedStringKey let mStr = NSMutableAttributedString() mStr.addAttribute(.foregroundColor, value: UIColor.white, range: NSRange())

    mStr.addAttributes([.font : UIFont()], range: NSRange())
  34. NSAttributedStringKey let mStr = NSMutableAttributedString() mStr.addAttribute(.foregroundColor, value: UIColor.white, range: NSRange())

    mStr.addAttributes([.font : UIFont()], range: NSRange()) "
  35. NSAttributedStringKey let item = UIBarButtonItem() let attr = [NSAttributedStringKey.foregroundColor.rawValue :

    UIColor.white] item.setTitleTextAttributes(attr, for: .normal)
  36. NSAttributedStringKey let item = UIBarButtonItem() let attr = [NSAttributedStringKey.foregroundColor.rawValue :

    UIColor.white] item.setTitleTextAttributes(attr, for: .normal) #
  37. Structures in AVFoundation • AVLayerVideoGravityResizeAspectFill -> AVLayerVideoGravity.resizeAspectFill

  38. Decodable • [SE-0166] Swift Archival & Serialization

  39. [SE-0166] Swift Archival & Serialization • Codable (Encodable & Decodableͷtypealias)ͷಋೖ

  40. [SE-0166] Swift Archival & Serialization • Codable (Encodable & Decodableͷtypealias)ͷಋೖ

  41. Himotoki

  42. Himotoki

  43. Decodable

  44. Decodable -> Himotoki.Decodable

  45. None
  46. None
  47. GEEK GARAGE