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

Migrating Objective-C to Swift

Migrating Objective-C to Swift

Sharing thoughts about migrating from Objective-C to Swift.
Why, How and What. This presentation was held on Munich iOS Meetup 2015/12/02. http://www.meetup.com/de/The-Munich-iOS-Developers-Meetup/events/226586543/

Elmar Kretzer

December 02, 2015
Tweet

More Decks by Elmar Kretzer

Other Decks in Programming

Transcript

  1. „…in future, all the 
 nice things will only 


    come to swift…“ Apple Software Engineer
  2. Types Autoclosures Structs Generics Implicit Member Expressions Custom Operators Typealiases

    Enums Currying Recursive Enumerations Property Observers Protocol Extensions Generators Subscripts Optionals Lazyness Pattern Matching Tuples Immutability ErrorType Access Control
  3. @warn_unused_result func flatMap<T> (@noescape _ transform: 
 (Self.Generator.Element) throws ->

    T?) 
 rethrows -> [T] Kinda Simplicity https://developer.apple.com/library/ios/documentation/Swift/Reference/ Swift_SequenceType_Protocol/index.html
  4. Bridging Header Add Imports as you need them Automatically generated

    by compiler Generated Header …using obj-c in swift… …using swift in obj-c…
  5. Try to avoid designing Swift for Obj-C You’ll have access

    to anything within a class or protocol that’s 
 marked with the @objc attribute as long as it’s compatible with Objective-C. 
 This excludes Swift-only features such as those listed here: Generics Tuples Enumerations defined in Swift without Int raw value type Structures defined in Swift Top-level functions defined in Swift Global variables defined in Swift Typealiases defined in Swift Swift-style variadics Nested types Curried functions https://developer.apple.com/library/ios/documentation/Swift/ Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/ uid/TP40014216-CH10-ID122
  6. Adding 
 New 
 Controller Refactor Existing Controller 1 2

    Where to use Swift 
 in the beginning? It`s ok to build a parallel universe
  7. struct Storyboard { struct Segues { static let channelInfo =

    "channelInfo" } struct Cells { static let channelCell = "channelCell" } struct ReuseView { static let sectionHeader = "header" } } Swift will make your life easier Define Segues, Identifier, and so forth
  8. Swift will make your life easier @IBOutlet weak var myCellView:

    UIView! { didSet { myCellView.backgroundColor = .redColor() myCellView.layer.masksToBounds = true myCellView.layer.cornerRadius = 2 } } Configure Views after outlet binding
  9. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { switch (segue.identifier, segue.destinationViewController,

    collectionView?.indexPathsForSelectedItems()?.first) { case let(Storyboard.Segues.info?, controller as InfoController, index?): controller.channel = channels?.get(index.row) default: break } } Swift will make your life easier Pattern Match Your Life
  10. Swift will make your life easier for (a, b) in

    (0...2).zipWithFollower() { print(a, b) // 0,1 1,2 2,nil } public struct ZipWithFollower<T: SequenceType>: SequenceType { public typealias Generator = AnyGenerator<(T.Generator.Element, T.Generator.Element?)> let sequence: T public init(_ sequence: T) { self.sequence = sequence } public func generate() -> Generator { var generator1 = sequence.generate() var generator2 = sequence.generate() _ = generator2.next() return anyGenerator { guard let element1 = generator1.next() else { return nil } return (element1, generator2.next()) } } } Clean up your control flow
  11. struct StateFlow<S,T> { let at: S let to: T ->

    S } let wizardFlow: [StateFlow<WizardState, WizardConfig>] = [ from(.Init).to { config in if config.useCustomSeletion { return .ComponentWizard } return .ComponentSelection } ] Swift will make your life easier Double Down on Types