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
340
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
130
Other Decks in Technology
See All in Technology
茨城の思い出を振り返る ~CDKのセキュリティを添えて~ / 20260201 Mitsutoshi Matsuo
shift_evolve
PRO
1
220
インフラエンジニア必見!Kubernetesを用いたクラウドネイティブ設計ポイント大全
daitak
0
340
Data Hubグループ 紹介資料
sansan33
PRO
0
2.7k
ClickHouseはどのように大規模データを活用したAIエージェントを全社展開しているのか
mikimatsumoto
0
210
GSIが複数キー対応したことで、俺達はいったい何が嬉しいのか?
smt7174
3
150
Amazon S3 Vectorsを使って資格勉強用AIエージェントを構築してみた
usanchuu
3
440
予期せぬコストの急増を障害のように扱う――「コスト版ポストモーテム」の導入とその後の改善
muziyoshiz
1
1.7k
(金融庁共催)第4回金融データ活用チャレンジ勉強会資料
takumimukaiyama
0
140
配列に見る bash と zsh の違い
kazzpapa3
1
120
データの整合性を保ちたいだけなんだ
shoheimitani
8
3k
AWS Network Firewall Proxyを触ってみた
nagisa53
0
190
顧客の言葉を、そのまま信じない勇気
yamatai1212
1
350
Featured
See All Featured
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
240
What's in a price? How to price your products and services
michaelherold
247
13k
The browser strikes back
jonoalderson
0
360
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
62
49k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
250
Color Theory Basics | Prateek | Gurzu
gurzu
0
200
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
We Are The Robots
honzajavorek
0
160
How to Think Like a Performance Engineer
csswizardry
28
2.4k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
The Invisible Side of Design
smashingmag
302
51k
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