What's new in Swift 3
-
Upload
marcio-klepacz -
Category
Software
-
view
268 -
download
1
Transcript of What's new in Swift 3
What’s new in Swift 3 22nd CocoaHeads SP @ Elo7 - Marcio Klepacz
@marciok
Agenda• Introduction
• Where to find Swift 3
• A Swift more “Swifty”
• New Foundation Value Types
• Safety Increased
• Coolness with SPM
• Conclusion
Introductionor what to expect from 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
Where to find 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.
A Swift more “Swifty”
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”.
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
Before:
After:func findCocoaheads(in city: String) { //...}
findCocoaheads(in:"São Paulo")
func findCocoaheadsIn(city: String) { //...}
findCocoaheadsIn("São Paulo")
Swift 2
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
New Foundation Value Types
New Foundation value types
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
Safety Increased
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
Coolness with SPM
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
Coolness with SPM
• Examples:
• SwiftyRuby - Calling Ruby from Swift
• SwiftyJava - Calling Java from Swift
Conclusion
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.
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/