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 Workshop
Search
puls
June 10, 2014
Technology
2
330
Swift Workshop
What you need to know about Apple's new language.
puls
June 10, 2014
Tweet
Share
More Decks by puls
See All by puls
Introductory Version Control with Git
puls
6
110
Other Decks in Technology
See All in Technology
20240416_devopsdaystokyo
kzkmaeda
1
190
検証を通して見えてきたTiDBの性能特性
lycorptech_jp
PRO
6
3.4k
小さな開発会社がWebサービスを作る理由
polidog
PRO
1
160
DevOpsメトリクスとアウトカムの接続にトライ!開発プロセスを通して計測できるメトリクスの活用方法
ham0215
2
200
TransitGatewayの基礎
toru_kubota
0
230
少数チームで挑む: SwiftUI, TCA, KMPを用いた 新規動画配信アプリ 「ABEMA Live」の開発について
tomu28
0
540
Google Cloud の AI を支える裏側のインフラを垣間見る!
maroon1st
0
200
オブザーバビリティの Primary Signals
onk
PRO
0
550
クラウドサインにおけるプロダクトマネージャーの役割と開発プロセス / 20240410_cloudsign-PdM
bengo4com
1
680
Data and AI Governance: Existing Challenges and Emerging Trends
scotthsieh825
0
160
Microsoft Cloudで開発ライフサイクルを保護する
kkamegawa
0
140
長期間TiDBを使ってきた話 @ 私たちはなぜNewSQLを使うのかTiDB選定5社が語る選定理由と活用LT / Experiences with TiDB Over Time
chibiegg
2
720
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
41
12k
GitHub's CSS Performance
jonrohan
1023
450k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
273
13k
It's Worth the Effort
3n
180
27k
Design by the Numbers
sachag
274
18k
Code Review Best Practice
trishagee
54
15k
What's new in Ruby 2.0
geeforr
337
31k
The Brand Is Dead. Long Live the Brand.
mthomps
48
28k
A better future with KSS
kneath
231
16k
Creatively Recalculating Your Daily Design Routine
revolveconf
209
11k
How to Ace a Technical Interview
jacobian
272
22k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
18
1.7k
Transcript
Swift Workshop June 10th, 2014
None
3 Swift is quite like Objective-C.
Native on iOS and OS X
Full support for all Cocoa and low-level APIs
Xcode- and LLVM-based workflow
Seamless bridging to Objective-C
Automatic Reference Counting
Named method parameters
Classes, Structs, Protocols, Enums
Closures
Int / Int32 / Int64 / UInt / UInt32 /
UInt64
if / for / while / do…while / switch
Swift is quite unlike Objective-C.
Not a superset of C
override func
Range operator
Range operator for i in 1..3 { print(i) } //
prints 12 for i in 1...3 { print(i) } // prints 123
Getters and setters
class Person { var firstName : String = "" var
lastName : String = "" var fullName : String { get { return "\(firstName) \(lastName)" } set { let parts = newValue.componentsSeparatedByString(" ") firstName = parts[0] lastName = parts[1] } } } Getters and setters
No default fallthrough on switch
Static type system
enum ComparisonResult { case Ascending case Equal case Descending }
! typealias Comparator = (Int, Int) -> ComparisonResult func sortArray(array : Int[], compareFunction : Comparator) { // ... } Static type system
Generics
enum ComparisonResult { case Ascending case Equal case Descending }
! func sortArray<T>(array : T[], compareFunction : (T, T) -> ComparisonResult) { // ... } Generics
Parameterized Enums
enum FlightStatus { case OnTime // Sweet case Delayed(Int) //
Oh well case UnitedAirlines // Should have known } Parameterized Enums
Structs with behavior
struct Rect { let height : Double let width :
Double var area : Double { return height * width } } ! let r = Rect(height: 4, width: 8) r.area // 32.0 Structs with behavior
Tuples
Pattern Matching
let a : Any = 124 let b = 346
! switch (a, b) { case (_, 345): println("b is 345") case (is Int, _): println("a is Int") default: println("nothing matched") } Pattern Matching
Constants and variables
let a = 1 var b = 2 b =
3 a = 3 // error ! let c = [1,2,3] // fixed-size array var d = [1,2,3] // mutable array c += 4 // error d += 4 ! c[1] = 4 // this works, though Constants and variables
Default parameter values
func madLibs(a : String, b : String = "Second", c
: String = "Third") { println("\(a) then \(b) then \(c)") } madLibs("foo", b: "bar", c: "baz") madLibs("yep") Default parameter values
Operator overloading
Subscripting
extension Int { subscript(index : Int) -> Int { get
{ return self % index } } } Subscripting
Value types
“Trailing closure” syntax
extension Int { func times(closure : () -> ()) {
for _ in 0..self { closure() } } } ! 3.times() { println("Swift is the best!") } “Trailing closure” syntax
Syntactic sugar
var numbers = [1,5,2,9,3,4,6,8,7] Syntactic sugar
var numbers = [1,5,2,9,3,4,6,8,7] numbers.sort({ a, b in a <
b }) Syntactic sugar
var numbers = [1,5,2,9,3,4,6,8,7] numbers.sort({ a, b in a <
b }) numbers.sort { a, b in a < b } Syntactic sugar
var numbers = [1,5,2,9,3,4,6,8,7] numbers.sort({ a, b in a <
b }) numbers.sort { a, b in a < b } numbers.sort { $0 < $1 } Syntactic sugar
var numbers = [1,5,2,9,3,4,6,8,7] numbers.sort({ a, b in a <
b }) numbers.sort { a, b in a < b } numbers.sort { $0 < $1 } numbers.sort(<) Syntactic sugar
Optionals
var a : Int? var b : Int ! a
// nil a = 1 // {Some 1} a + 2 // this is an error a! + 2 // 3 b = 2 // 2 b + 2 // 4 Optionals
Playgrounds
Practical Swift
Value types vs. Reference types
Structs vs. Classes
Array vs. NSArray
array.unshare()
http://terribleswiftideas.tumblr.com
http://developer.apple.com/swift
None
None