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

Amy Dyer: Incremental Swift

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
September 01, 2016

Amy Dyer: Incremental Swift

Bio:
Amy is a staff software engineer at Etsy in Brooklyn, NY. She's been an iOS developer for 5 years and is currently working on Etsy's app for shoppers.

Abstract:
What do you do when you’re ready to upgrade to Swift, but rewriting your existing Objective-C apps isn’t an option? Using Etsy as a case study, I'll discuss a blueprint for integrating Swift incrementally into your apps. Swift provides rich features for Objective-C interoperability, but applying them to your current codebase isn’t always straightforward. We’ll cover technical details, such as linting and managing dependencies, as well as organizational strategies for gathering support, and other things we’ve learned at Etsy along the way. You’ll be prepared for a smooth transition to Swift: both in your code and in your company.

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

September 01, 2016
Tweet

Transcript

  1. Incremental Swift

  2. Agenda Where we began Experiments in Swift Lessons learned

  3. Etsy is a global marketplace where people around the world

    connect, both online and offline, to make, sell and buy unique goods. 3
  4. 4 SELLERS: Make Mobile Easy BUYERS: Simple & Social Apps

  5. Swift Let’s talk about 5

  6. The Magic of Swift 6

  7. No Swift Stick with Objective C Re-write everything 
 in

    Swift All Swift 7
  8. “Let’s re-write everything!” 8

  9. 9

  10. “How can you prove that Swift code would be any

    better than the code we already have?” 10
  11. 5 years of commits 280,000+ lines of Objective C 2,500+

    implementation files Etsy for iOS as of August 2016 11
  12. Finding the answer 12

  13. No Swift Stick with Objective C Re-write everything 
 in

    Swift All Swift 13 Let’s write our tests 
 in Swift Tests in Swift
  14. Epiphany 14

  15. “Swift will let us write safer code that crashes less.”

    15
  16. 16

  17. “Let’s make Swift development possible instead of mandatory.” 17

  18. No Swift Stick with Objective C Re-write everything 
 in

    Swift All Swift 18 Mix Swift 
 and Objective C Some Swift
  19. Disadvantages • In-between codebase • Developers need to know two

    languages • Need to handle interoperability features 19
  20. Advantages • Slower change means more time to adapt to

    risks • Explore issues unique to our apps • Learn Swift as an organization 20
  21. Swift by experiment 21

  22. Know that things will go wrong 22

  23. Evaluate the ecosystem 23 App Crash Logger Build
 machines Translations

    Libraries Apple ???
  24. Goals 1 2 3 Add the Swift runtime A/B test

    our first
 Swift class Develop new 
 features in Swift 24
  25. Adding the Swift runtime Step one 25

  26. Ship code that doesn’t run 26

  27. Lesson learned: Check your SwiftSupport folder 27

  28. Lesson learned: Monitoring build sizes 28

  29. Running our first Swift code Step two 29

  30. A/B testing our first Swift class 30 @interface MyClass :

    NSObject - (void)myFunction; @end class MyClass : NSObject { func myFunction() -> Void { . . . } }
  31. Pop quiz! 31

  32. guard let collection = self.collection else { return } let

    isPrivate = collection.isPrivate() let isFavorites = collection.type == “favorites” Where does this code crash? 32
  33. guard let collection = self.collection else { return } let

    isPrivate = collection.isPrivate() let isFavorites = collection.type == “favorites” collection.type : String! Where does this code crash? 33
  34. Lesson learned: Annotate while you import 34 // Use this

    file to import your target’s public headers that you // would like to expose to Swift. // // NOTE: Before importing your file here, make sure you’ve added // nullability (null/nonnull) specifiers to both that file AND // any headers it directly imports. // // Only *you* can prevent implicitly unwrapped optionals.
  35. Tip: Demangle crash logs 35 > xcrun swift-demangle _TToFC4Etsy6Crashy9unwrapNilfS0_FT_T_ _TToFC4Etsy6Crashy9unwrapNilfS0_FT_T_

    ---> @objc Etsy.Crashy.unwrapNil (Etsy.Crashy) -> () -> ()
  36. Writing new code in Swift Step three 36

  37. “Don’t write any Swift that another developer would 
 have

    to re-write to use 
 from Objective C” Team Goal 37
  38. Non-backwards compatible features • Generics • Tuples • Enumerations with

    a raw type other than Int • Structures • Top-level functions • Global variables • Typealiases • Variadic function parameters • Nested types • Curried functions 38
  39. Tip: use access levels 39 public internal private fileprivate

  40. Tip: Enforce interoperability with a linter 40 Code/Swift/Interoperability.swift:19:2: warning: Objective-C

    Interoperability Violation: Object 'someFunction(_:_:)' of type FunctionFree should be private, but is internal (objective_c_interoperability)
  41. Tip: Add Swift to prettify your Objective C 41 WWDC

    2015: “Improving Your Existing Apps with Swift” @interface MyClass : NSObject - (void)anUglyFunction NS_REFINED_FOR_SWIFT; @end extension MyClass { public func aPrettierFunction() -> Void { return self.__anUglyFunction() } }
  42. Education, standardization and the future 42

  43. Lesson learned: standardize on a version 43 > swift -version

    Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.81)
  44. Tip: Borrow some code standards • Expand on existing code

    standards • Keep them up to date as you learn and change 44
  45. Education 45

  46. Adopting future versions 46

  47. The Bigger Picture 47

  48. Find a raison d’être 48

  49. Start outside your code base 49

  50. Make and test hypotheses 50

  51. You can do it! 51

  52. Thank you 52