What's new in Swift 3

23
What’s new in Swift 3 22nd CocoaHeads SP @ Elo7 - Marcio Klepacz @marciok

Transcript of What's new in Swift 3

Page 1: What's new in Swift 3

What’s new in Swift 3 22nd CocoaHeads SP @ Elo7 - Marcio Klepacz

@marciok

Page 2: What's new in Swift 3

Agenda• Introduction

• Where to find Swift 3

• A Swift more “Swifty”

• New Foundation Value Types

• Safety Increased

• Coolness with SPM

• Conclusion

Page 3: What's new in Swift 3

Introductionor what to expect from Swift 3

Page 4: What's new in Swift 3

Swift 3

• Was also brought by proposals from the community.

• Is focused on clarity, at the point of use.

• Brings more clarity when working with legacy APIs

• Safer

Page 5: What's new in Swift 3

Where to find Swift 3

Page 6: What's new in Swift 3

Where to find Swift 3

• Xcode 8 (Beta)

• swift.org snapshots

Page 7: What's new in Swift 3

Considerations

• You can only submit apps using Swift 3 when Xcode GM is shipped ~end of 2016.

• Swift 3 changes everything. Xcode 8 comes with a code migration tool (can migrate to 2.3). However, I would still check the migrations made by Xcode.

Page 8: What's new in Swift 3

A Swift more “Swifty”

Page 9: What's new in Swift 3

Swift more “Swifty”

• Not terse or Verbose code.

• Clarity is more important than brevity.

• Concise code is a consequence of using contextual cues.

• What they mean (In my option) is: Let’s ditch legacy ObjC API designs, and be more “Swifty”.

Page 10: What's new in Swift 3

Before:

After:

NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, …)

NSTimer.scheduledTimer(timeInterval: 0.35, target: self, …)

UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)

UIFont.preferredFont(forTextStyle: UIFontTextStyleSubheadline)

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?

func viewForZooming(in scrollView: UIScrollView) -> UIView?

Swift 2

Swift 3

Page 11: What's new in Swift 3

Before:

After:func findCocoaheads(in city: String) { //...}

findCocoaheads(in:"São Paulo")

func findCocoaheadsIn(city: String) { //...}

findCocoaheadsIn("São Paulo")

Swift 2

Swift 3

Page 12: What's new in Swift 3

Before:

After:

UIColor.blueColor()

UIColor.blue() // or simply .blue() if the compile can infer

cocoaheads(insert: “São Paulo” atIndex: 0)

cocoaheads(insert: “São Paulo” at: 0)

NSUserDefaults.standardUserDefaults()

UserDefaults.standard() // NSUserDefaults was renamed to UserDefaults

Swift 2

Swift 3

Omitting needless words

Page 13: What's new in Swift 3

New Foundation Value Types

Page 14: What's new in Swift 3

New Foundation value types

Page 15: What's new in Swift 3

Example

let dateComponent = NSDateComponents()dateComponent.year = 2016let anotherComponent = dateComponentanotherComponent.year = 2017 //`dateComponent` year was also changed to 2017

var dateComponent = DateComponents()dateComponent.year = 2016 // It will warn you if `dateComponent` is a `let`let anotherComponent = dateComponentanotherComponent.year = 2017 //`dateComponent` is still 2016

Before: Swift 2

After: Swift 3

Page 16: What's new in Swift 3

Safety Increased

Page 17: What's new in Swift 3

Safety IncreasedBefore:

After:

var cocoaheads = [String!]()cocoaheads.append("São Paulo")let sp = cocoaheads[0]sp.dynamicType // ImplicitlyUnwrappedOptional<String>

Swift 2

Swift 3

var cocoaheads = [String!]()cocoaheads.append("São Paulo")let sp = cocoaheads[0]sp.dynamicType // Optional<String>let dangerousSp: String! = cocoaheads[0]dangerousSp.dynamicType // ImplicitlyUnwrappedOptional<String>

No more `ImplicitlyUnwrappedOptional` propagation

Page 18: What's new in Swift 3

Coolness with SPM

Page 19: What's new in Swift 3

Coolness with SPM

• The ability to pass custom flags during linker invocations. Also on C and Swift compilation.

• Export as dynamic library

==

• Easy to Interoperate with other languages

Page 20: What's new in Swift 3

Coolness with SPM

• Examples:

• SwiftyRuby - Calling Ruby from Swift

• SwiftyJava - Calling Java from Swift

Page 21: What's new in Swift 3

Conclusion

Page 22: What's new in Swift 3

Conclusion• Swift 3 changes everything (again). It will force

us get rid of our ObjC muscle muscle memory.

• Was also brought by the community. We can always go on Github to complain, suggest and change.

• There are many others news things that weren’t mentioned at this presentation, so don’t forget to check online for other things.

Page 23: What's new in Swift 3

Thanks!• Twitter: @marciok

• Github: /marciok

• References:

• https://www.raywenderlich.com/135655/whats-new-swift-3

• https://www.bignerdranch.com/blog/wwdc-2016-increased-safety-in-swift-3/

• https://developer.apple.com/videos/play/wwdc2016/402/