Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Swift 3 - From Expert to Beginner
Search
Wei Wang
July 21, 2016
Programming
2
210
Swift 3 - From Expert to Beginner
A speech for changes in Swift 3.
Wei Wang
July 21, 2016
Tweet
Share
More Decks by Wei Wang
See All by Wei Wang
網路之難,難於上青天 - iPlayground 2019
onevcat
11
4.9k
GMTC 2019 - 在分歧中发展,2019 我们能用 Swift 做什么
onevcat
0
900
从 Swift 到机器学习
onevcat
2
930
iOS Dev - The Dark Side
onevcat
0
120
面向协议编程与 Cocoa 的邂逅
onevcat
14
4.7k
如何打造一个让人愉快的框架
onevcat
4
22k
JSPatch Introduction
onevcat
0
180
Objective-C Runtime Swizzle
onevcat
0
170
Unity Memory
onevcat
0
130
Other Decks in Programming
See All in Programming
layerx_20241129.pdf
kyoheig3
2
290
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
350
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
200
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
170
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
180
Zoneless Testing
rainerhahnekamp
0
120
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
あれやってみてー駆動から成長を加速させる / areyattemite-driven
nashiusagi
1
200
N.E.X.T LEVEL
pluu
2
300
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
A Modern Web Designer's Workflow
chriscoyier
693
190k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
The World Runs on Bad Software
bkeepers
PRO
65
11k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
GraphQLとの向き合い方2022年版
quramy
44
13k
Producing Creativity
orderedlist
PRO
341
39k
Transcript
SP TF JP20028 - onevcat Wei Wang 1
Recently 2
Swift From Beginner to Expert JP20028 - onevcat, Nov 2014
3
Swift 3 From Expert to Beginner JP20028 - onevcat, Jul
2016 4
Open + Evolution 5
Swift Evolution Repo1 SE-XXXX New Site: https://apple.github.io/ swift-evolution/ 1 https://github.com/apple/swift-evolution
6
7
Important proposals & Broken APIs 8
SE-0023 API Design Guidelines And SE-0006 Apply API Guidelines to
the Standard Library SE-0005 Better Translation of Objective-C APIs Into Swift SE-0033 Import Objective-C Constants as Swift Types 9
Problems of Current Cocoa APIs • Disordered • Verbose &
Duplicated • For Objective-C. Not Swifty 10
Clarity let text = "Hello world" text.stringByAppendingString(", Swift2") 11
Clarity public func stringByAppendingString(aString: String) -> String 12
Clarity public func stringByAppendingString(aString: String) -> String 13
Clarity public func appending(_ aString: String) -> String 14
Clarity // Swift 2 text.stringByAppendingString(", Swift2") // Swift 3 text.appending(",
Swift3") 15
More Examples // Swift 2 let color = UIColor.blackColor() let
app = UIApplication.sharedApplication() // Swift 3 let color = UIColor.black() // view.backGroundColor = .black() <- Type Infer let app = UIApplication.shared 16
More Examples var array = [1, 2, 3, 4] //Swift
2 array.removeAtIndex(1) // Swift 3 array.remove(at: 1) 17
More Examples let text = "abcde" // Swift 2 text.stringByReplacingOccurrencesOfString(
"abc", withString: "123") // Swift 3 text.replacingOccurrences( of: "abc", with: "123") 18
Exception // Remove KVO observer // Swift 2 app.removeObserver(self, forKeyPath:
"valueKey") // Swift 3 app.removeObserver(self, forKeyPath: "valueKey") // Why not? app.remove(observer: self, for: "valueKey") 19
Exception func removeObserver( _ observer: NSObject, forKeyPath keyPath: String )
When NSObject, Any, AnyObject, Int, String included in method signature: • Declaration is fine. • But using is not so clear. 20
From Swift 2 To 3 Get to be used to
new APIs With Swift migrator Difficulty: ! 21
SE-0088 Modernize libdispatch for Swift 3 naming conventions 22
// Swift 2 let queue = dispatch_queue_create( "my.queue", DISPATCH_QUEUE_CONCURRENT) dispatch_async(queue)
{ doHeavyWork() dispatch_async(dispatch_get_main_queue(), { updateUI() }) } dispatch_after( dispatch_time( DISPATCH_TIME_NOW, Int64(3500 * NSEC_PER_SEC)), dispatch_get_main_queue()) { call() } 23
// Swift 3 let queue = DispatchQueue( label: "my.queue", attributes:
.concurrent) queue.async { doHeavyWork() DispatchQueue.main.async { updateUI() } } let time = DispatchTime.now() + 3.5 DispatchQueue.main.after(when: time) { call() } // Also for group, semaphore, IO etc. 24
Not only GCD... // Swift 2 func rotationAround(offset: CGPoint, angle:
CGFloat, transform: CGAffineTransform = CGAffineTransformIdentity) -> CGAffineTransform { var result = CGAffineTransformTranslate(transform, offset.x, offset.y) result = CGAffineTransformRotate(result, angle) return CGAffineTransformTranslate(result, -offset.x, -offset.y) } 25
// Swift 2 func rotationAround(offset: CGPoint, angle: CGFloat, transform: CGAffineTransform
= CGAffineTransformIdentity) -> CGAffineTransform { var result = CGAffineTransformTranslate(transform, offset.x, offset.y) result = CGAffineTransformRotate(result, angle) return CGAffineTransformTranslate(result, -offset.x, -offset.y) } // Swift 3 func rotationAround(offset: CGPoint, angle: CGFloat, transform: CGAffineTransform = .identity) -> CGAffineTransform { return transform.translateBy(x: offset.x, y: offset.y) .rotate(angle) .translateBy(x: -offset.x, y: -offset.y) } 26
// Swift 2 func trace(in context: CGContext, path: CGPath) {
let red = CGColorCreateGenericRGB(1, 0, 0, 1) CGContextSaveGState(context) CGContextAddPath(context, path) CGContextSetStrokeColorWithColor(context, red) CGContextStrokePath(context) CGContextRestoreGState(context) } // Swift 3 func trace(in context: CGContext, path: CGPath) { let red = CGColor.red context.saveGState() context.addPath(path) context.setStrokeColor(red) context.strokePath() context.restoreGState() } 27
Imported by Compiler from C No Runtime Overhead 28
From Swift 2 To 3 Just forget the legacy C
Difficulty: ! 29
SE-0069 Mutability and Foundation Value Types SE-0086 Drop NS Prefix
in Swift Foundation 30
Value Type is Good 31
Value Type is Good • Immutability • Less State •
Performance 32
Struct • String • Array Class • NSURL • NSDate
• NSData 33
Swift 3 All NS prefix removed: • NSURL → URL
• NSDate → Date • NSData → Data • etc. From Class to Struct. Not an NSObject anymore. 34
public struct URL : ReferenceConvertible, CustomStringConvertible, Equatable { public typealias
ReferenceType = NSURL //... } • Reference type underneath • Value Semantic • Copy-on-Write 35
Still be a class: • NSNotificationCenter → NotificationCenter • NSUserDefaults
→ UserDefaults • etc. 36
From Swift 2 To 3 No NS prefix needed Difficulty:
! 37
SE-0046 Establish consistent label behavior across all parameters including first
labels 38
Swift 2 The first label is omitted by default. func
foo(a: T, b: U, c: V) { } foo(a, b: b, c: c) 39
Swift 3 You need to write the first label by
default: func foo(a: T, b: U, c: V) { } foo(a: a, b: b, c: c) 40
Swift 3 Back compatibility Your current API will be imported
as: func foo(_ a: T, b: U, c: V) { } But you should consider in Swift 3 way and give it a fix. 41
SE-0004 Remove the ++ and -- operators 42
let a = ++x // Plus self (x), then use
let b = x++ // Use, then plus let c = --x // Minus, then use let d = x-- // Use, then minus 43
let a = ++x // Plus self (x), then use
let b = x++ // Use, then plus let c = --x // Minus, then use let d = x-- // Use, then minus var i = 0 if shouldCount { i++ } 44
let a = ++x // Plus self (x), then use
let b = x++ // Use, then plus let c = --x // Minus, then use let d = x-- // Use, then minus (i++)+(++i)+(++i)+(i++)+(i++) = ? // Good luck! 45
From Swift 2 To 3 Stop using ++ and --
Use += instead var i = 0 if shouldCount { i += 1 } Difficulty: ! 46
SE-0025 Scoped Access Level 47
Swift 2 public internal private ← current file 48
Swift 3 public internal fileprivate ← current file private 49
Swift 3 public internal fileprivate private ← current scope 50
// Sample.swift struct MyStruct { private var name: String? func
sayHello() { print("How are you?") } private func damnIt() { print("Damn you!") } // Cannot use `sayMorning` } extention MyStruct { // Cannot use `damnIt` and `name` private func sayMorning() { print("Morning!") sayHello() } } 51
From Swift 2 To 3 Rename private to fileprivate Use
private when really needed Difficulty: ! 52
SE-0047 Defaulting non-Void functions so they warn on unused results
53
In Swift 2 @warn_unused_result(mutable_variant="sortInPlace") public func sort() -> [Self.Generator.Element] 54
In Swift 3 @discardableResult func youCanIgnoreTheReturnValue() -> Int From Swift
2 To 3 Use @discardableResult according to compile warning. Difficulty: ! 55
Bad News 56
• Full of breaking changes • From beginner to expert,
again • Third party support 57
Good News 58
Xcode 8 contains both Swift 2.3 and Swift 3 59
Is it worth upgrading to Swift 3? 60
It depends. 61
Thanks 62