migrating objective-c to swift

29
func migrating(from: ObjC) -> Swift! [objC autorelease]

Upload: elmar-kretzer

Post on 18-Jan-2017

202 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Migrating Objective-C to Swift

func migrating(from: ObjC) -> Swift!

[objC autorelease]

Page 2: Migrating Objective-C to Swift

Why?

Page 3: Migrating Objective-C to Swift

https://twitter.com/clattner_llvm

Page 4: Migrating Objective-C to Swift

WWDC Appearances

swift objective-c

2014 19 632015 71 15

Page 5: Migrating Objective-C to Swift

„…in future, all the nice things will only

come to swift…“Apple Software Engineer

Page 6: Migrating Objective-C to Swift

What will you loseon the way?

Page 7: Migrating Objective-C to Swift

* [ ] .h .mGood old Friends

Page 8: Migrating Objective-C to Swift

Runtime Dynamics

NSInvocation NSMethodSignature

[obj load] nil messaging

Macros

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

Page 9: Migrating Objective-C to Swift

What will you gain?

Page 10: Migrating Objective-C to Swift

Types

Autoclosures

Structs

Generics

Implicit Member Expressions

Custom Operators

Typealiases

Enums

Currying

Recursive Enumerations

Property ObserversProtocol Extensions

Generators

Subscripts

Optionals

Lazyness

Pattern Matching

Tuples

Immutability

ErrorType

Access Control

Page 11: Migrating Objective-C to Swift

Simplicity

view.backgroundColor = .redColor()

[view setBackgroundColor: [UIColor redColor]]

Page 12: Migrating Objective-C to Swift

@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

Page 13: Migrating Objective-C to Swift

https://github.com/typelift/Swiftz/blob/master/Swiftz/EitherExt.swift

Not-So-Simplicity-At-All

Page 14: Migrating Objective-C to Swift

What will remain?

Page 15: Migrating Objective-C to Swift

SDKXYZKit

Model View ControllerRetain Cycles

Xcode

Page 16: Migrating Objective-C to Swift

How to start?

Page 17: Migrating Objective-C to Swift

Existing Objective-C ProjectNo Warnings 😇

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/index.html#//apple_ref/doc/uid/TP40014216-CH2-ID0

Using Swift with Cocoa and Objective-C

Page 18: Migrating Objective-C to Swift

Bridging HeaderAdd Imports as you need them

Automatically generated by compilerGenerated Header

…using obj-c in swift…

…using swift in obj-c…

Page 19: Migrating Objective-C to Swift

Optimize Obj-C for Swift

NS_ASSUME_NONNULL_BEGIN nonnull|nullable|null_unspecified

NSArray<__kindof UIView *> *someViews;

https://developer.apple.com/videos/play/wwdc2015-401/

Page 20: Migrating Objective-C to Swift

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

Page 21: Migrating Objective-C to Swift

Adding New Controller

Refactor Existing Controller1 2

Where to use Swift in the beginning?

It`s ok to build a parallel universe

Page 22: Migrating Objective-C to Swift

Find New Solutions™ and yeah, use guard…

Page 23: Migrating Objective-C to Swift

Swift will makeyour life easier

title = ~"Select Title"

Custom Operator for i18n

Page 24: Migrating Objective-C to Swift

struct Storyboard { struct Segues { static let channelInfo = "channelInfo" } struct Cells { static let channelCell = "channelCell" } struct ReuseView { static let sectionHeader = "header" } }

Swift will makeyour life easier

Define Segues, Identifier, and so forth

Page 25: Migrating Objective-C to Swift

Swift will makeyour life easier

@IBOutlet weak var myCellView: UIView! { didSet { myCellView.backgroundColor = .redColor() myCellView.layer.masksToBounds = true myCellView.layer.cornerRadius = 2 } }

Configure Views after outlet binding

Page 26: Migrating Objective-C to Swift

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 makeyour life easier

Pattern Match Your Life

Page 27: Migrating Objective-C to Swift

Swift will makeyour 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

Page 28: Migrating Objective-C to Swift

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 makeyour life easier

Double Down on Types

Page 29: Migrating Objective-C to Swift

@elmkretzer

Go Swift!

www.symentis.com