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

Amy Dyer: Incremental Swift

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.

Realm

September 01, 2016
Tweet

More Decks by Realm

Other Decks in Technology

Transcript

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

    connect, both online and offline, to make, sell and buy unique goods. 3
  2. 9

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

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

    implementation files Etsy for iOS as of August 2016 11
  5. No Swift Stick with Objective C Re-write everything 
 in

    Swift All Swift 13 Let’s write our tests 
 in Swift Tests in Swift
  6. 16

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

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

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

    risks • Explore issues unique to our apps • Learn Swift as an organization 20
  10. Goals 1 2 3 Add the Swift runtime A/B test

    our first
 Swift class Develop new 
 features in Swift 24
  11. A/B testing our first Swift class 30 @interface MyClass :

    NSObject - (void)myFunction; @end class MyClass : NSObject { func myFunction() -> Void { . . . } }
  12. guard let collection = self.collection else { return } let

    isPrivate = collection.isPrivate() let isFavorites = collection.type == “favorites” Where does this code crash? 32
  13. 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
  14. 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.
  15. “Don’t write any Swift that another developer would 
 have

    to re-write to use 
 from Objective C” Team Goal 37
  16. 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
  17. 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)
  18. 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() } }
  19. Lesson learned: standardize on a version 43 > swift -version

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

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