Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checklist for migrating to idiomatic Swift 2 (AKA where is the Swift 2 transition guide)?

I've been trying to locate a transition guide for Swift 2, in particular things developers should be aware of when migrating Swift 1/1.2 codebases over to Swift 2. Obviously you have the migration assistant in Xcode, but that only really covers the donkey work and not the stuff that requires a bit more intelligent thought.

Based on the resources I was able to find on Swift 2, I've put together the following checklist:

  • try/catch/throw error handling - to be used for recoverable errors; revise error handling code accordingly. In particular, check all uses of NSError and calling back to delegates to report recoverable errors.
  • Use enums conforming to ErrorType to define your own meaningful errors.
  • Use #available for accessing newer platform APIs - check API use against app Deployment Target and revise accordingly
  • protocol extensions - move as much code as possible into these to aid re-use. In particular refactor Global Functions into protocol extensions.
  • nullability annotations & generics - remove redundant optional bindings and type castings
  • Use do { } to control scope and free large resources early
  • Move old do { ... } while loops to repeat { } (to remove ambiguity and improve readability)
  • Use guard to return early and avoid excessive indentation
  • Use defer for cleanup code like closing files etc.
  • Use Option Sets rather than OR-ing values together (e.g. viewAnimationOptions = [.Repeat, .CurveEaseIn, .TransitionCurlUp])
  • Review public accessor specifiers which were previously only required to support testing. Use @testable and import MyApp instead.
  • Move single-case switch statements to the new if case .MyEnumCase(let value) = bar() where value != 42 { doThing(value) }
  • Use "for ... in" filtering to clean up for loops containing if filtering statements e.g. for value in mySequence where value != "" { }
  • native support for C function pointers - provide using closures or global functions (do not capture local context when doing so)
  • fix any new let/var warnings
  • fix any unused variable warnings
  • Failable initializers can now return nil before calling super.init - remove any previous workarounds required. Designated initializers still have to initialize all stored properties before returning nil however.

Sources:

https://developer.apple.com/swift/blog/?id=29

https://developer.apple.com/swift/

https://developer.apple.com/library/prerelease/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc7_release_notes.html#//apple_ref/doc/uid/TP40001051-CH5-SW1

https://developer.apple.com/videos/wwdc/2015/?id=106

http://www.raywenderlich.com/108522/whats-new-in-swift-2

What have I missed?

Part of the problem is that Swift 2 has continued to evolve past WWDC. So even this year's WWDC videos are already potentially out of date, or at least not the whole story.

like image 288
Andrew Ebling Avatar asked Sep 17 '15 08:09

Andrew Ebling


1 Answers

Unfortunately, at this time there is no official "transition guide" from Apple as such.

The Swift Programming Language (Swift 2) is always updated by Apple whenever they release a new version of Swift and is therefore one of the best sources for up to date information about Swift 2 (or later). There is plenty of explanation and example code of the entire language, not just the changes, but this is definitely at least on of the best sources for the information you are looking for right now.

like image 102
Max Goodridge Avatar answered Sep 29 '22 15:09

Max Goodridge