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
120
Other Decks in Technology
See All in Technology
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
290
.NET 9 のパフォーマンス改善
nenonaninu
0
1k
多領域インシデントマネジメントへの挑戦:ハードウェアとソフトウェアの融合が生む課題/Challenge to multidisciplinary incident management: Issues created by the fusion of hardware and software
bitkey
PRO
2
110
祝!Iceberg祭開幕!re:Invent 2024データレイク関連アップデート10分総ざらい
kniino
3
310
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1.1k
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
580
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
0
210
生成AIのガバナンスの全体像と現実解
fnifni
1
190
サイバー攻撃を想定したセキュリティガイドライン 策定とASM及びCNAPPの活用方法
syoshie
3
1.3k
レンジャーシステムズ | 会社紹介(採用ピッチ)
rssytems
0
180
権威ドキュメントで振り返る2024 #年忘れセキュリティ2024
hirotomotaguchi
2
760
Amazon Kendra GenAI Index 登場でどう変わる? 評価から学ぶ最適なRAG構成
naoki_0531
0
120
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
98
Scaling GitHub
holman
458
140k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
4 Signs Your Business is Dying
shpigford
181
21k
How GitHub (no longer) Works
holman
311
140k
Building an army of robots
kneath
302
44k
Building Your Own Lightsaber
phodgson
103
6.1k
GraphQLとの向き合い方2022年版
quramy
44
13k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Designing for Performance
lara
604
68k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
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