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
Types in Swift
Search
yashigani
February 28, 2015
Programming
5
2.3k
Types in Swift
Cocoa勉強会関西 #60の発表資料です
yashigani
February 28, 2015
Tweet
Share
More Decks by yashigani
See All by yashigani
Hatena Engineer Seminar #10
yashigani
0
2.2k
Dependency Injection in iOS Apps
yashigani
2
460
Multiple layout with UITableViewCell
yashigani
1
3.4k
Lazy stored property
yashigani
3
2.1k
Code reviewing in iOS app development
yashigani
9
4.4k
Useful 3 techniques developing in iOS Apps
yashigani
0
1.5k
Introduction to WebKitPlus
yashigani
2
2.3k
Swift with apps by Hatena
yashigani
13
29k
What's new WebKit.framework
yashigani
2
1.4k
Other Decks in Programming
See All in Programming
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
670
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
Click-free releases & the making of a CLI app
oheyadam
2
120
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
900
Contemporary Test Cases
maaretp
0
140
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
CSC509 Lecture 09
javiergs
PRO
0
140
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
110
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
How to Ace a Technical Interview
jacobian
276
23k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
89
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
BBQ
matthewcrist
85
9.3k
Practical Orchestrator
shlominoach
186
10k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Designing for Performance
lara
604
68k
For a Future-Friendly Web
brad_frost
175
9.4k
Transcript
ܕ <UZQF> @yashigani Cocoaษڧձؔ #60
yashigani id:yashigani_w @yashigani Mobile Application Engineer http://yashigani.hatenablog.com Hatena
https://flic.kr/p/nBzE6V ܕʹϋϚͬͨ͜ͱ͋Γ·͔͢ • ίϯύΠϧΤϥʔʹͳΔͭͰ͠ΐʁ • ܕ੍ͬͯͳ͚ͩͰͱʹ͔͘अຐ • id࠷ߴΜʂʂʂܕͱ͔͍ΒΜʂʂʂ
ܕͬͯԿʁ
ܕৼΔ͍
ৼΔ͍ • ৼΔ͍ = σʔλߏ + ॲཧ • Ͳ͏͍͏σʔλ͕Ͳ͏ಈ࡞͢Δ͔༧Ͱ͖Δ •
ΠϯλʔϑΣʔε͕ݫ֨
$
ΞυϨεͷϝλσʔλ • ϝϞϦۭؒʹ͓͚ΔมͷҐஔ • ϨΠϠͷॲཧ͕Մೳͳނ • ϓϦϛςΟϒ͗͢Δ
0CKFDUJWF$
ৼΔ͍ͷϝλσʔλ • isaϙΠϯλ • ม͕ৼΔ͏͖͜ͱΛࣔࠦ(ಈతσΟεύον) • ͨͩ͠ඞͣ͠ै͑ΔͱݶΒͳ͍ • มͷৼΔ͍Λଋറ͢ΔͷͰແ͍
4XJGU
ৼΔ͍Λଋ͢Δͷ • ৼΔ͍Λมʹଋറ • ͋ΔܕͷมͦͷܕͷৼΔ͍͔͠͠ͳ͍ • ܕͷ͋Δϓϩάϥϛϯά͕Մೳ
ͳͥϓϩάϥϜʹ ܕ͕ඞཁͳͷ͔
ܕ ܕ͕͋Δ͔ΒɺܕഁΓɻ ܕ͕ͳ͚Ε୯ͳΔʮܗແ͠ʯ ʕेീதଜצࡾ Վبऀ
/4%JDUJPOBSZ
NSDictionary *user = //... NSString *name = user[@"name"];
NSDictionary *user = //... NSString *name; if ([user[@"name"] isKindOfClass: [NSString
class]]) { //!!!!! name = user[@"name"]; } else { //!!!!! }
https://flic.kr/p/7BD2UE Too bad ߏ͕ෆ໌ͳͷͰσʔλͷଘࡏΛஅఆͰ͖ͳ͍ Կ͕ೖ͍ͬͯΔͷ͔ෆ໌ͳͷͰσʔλܕΛࢦఆ ৼΔ͍͕ແ͍
ܕ͕ແ͍
.PEFM0CKFDU JO0CKFDUJWF$
@interface User : NSObject @property (copy) NSString *name; @property NSUInteger
age; @property (getter=isPremium) BOOL premium; @end
User *user = //... UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@" cell"];
cell.textLabel.text = user.name;
User *user = //... UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@" cell"];
if (user.name != nil) { cell.textLabel.text = user.name; } else { //!!!!! }
https://flic.kr/p/i74K6M Too bad σʔλͷߏͱܕදݱͰ͖ͨ ΠϯλʔϑΣʔε͕ෆఆ(nilͷՄೳੑ) ৼΔ͍͚͕ͩ͋Δ(ଋറ͕ແ͍)
ܗແ͠
.PEFM0CKFDU JO4XJGU
struct User { let name: String let age: Int let
isPremium: Bool }
let user = //... let cell = tableView.dequeueReusableCellWithId entifier("cell") cell?.titleLabel.text
= user.name
https://flic.kr/p/egaNwP Relaxed ෆ֬ఆͳཁૉϏδωεϩδοΫʹӅṭ ΞϓϦέʔγϣϯσʔλΛදࣔ͢Δ͚ͩ ৼΔ͍Λଋറ͢Δ͜ͱͰଋ͞Εͨڍಈ
5IJTJTܕ
ܕͷ͋ΔϓϩάϥϜ • ৼΔ͍͕໌֬Ͱશ • ͕ͨͬͯ͠ෆඞཁͳ৺͕͍Βͳ͍ • ϏδωεϩδοΫͱΞϓϦέʔγϣϯͷ • ΠϯλʔϑΣʔεͷΈʹґଘ࣮ͨ͠
0OFNPSFUIJOH
func parse(JSON: AnyObject?) -> (User?, NSError?) let JSON: AnyObject? =
//... let (user, error) = parse(JSON) if let e = error { //... return } if let u = user { //... } else { // maybe user is nil... }
https://flic.kr/p/DC3Q Too bad usererrornilͷͱ͖Ͳ͏͍͏Τϥʔʁ ͬͱͬͱstrictʹදݱ͍ͨ͠ (User?, NSError?)ૉ͗͢Δ
ܕΛഁΔ ܕʹΑΔܕͷநԽ
3FTVMU5
enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)
{ self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } }
ܕഁΓٕͦͷ ʙHFOFSJDTʙ
enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)
{ self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } } generics
ҦΘϫΠϧυΧʔυܕ • ͱΓ͋͑ͣͳΜͰೖΕΒΕΔܕ • ಉ͡ϥϕϧͷͷಉ͡ܕ • ΠϯλʔϑΣʔεΛܾΊͨͱ͖ʹܕ͕ܾ·Δ • func parse(_:AnyObject)
-> Result<User> • Any/AnyObjectͱҧͬͯղ͘ͱ͖ʹͳʹ͔ ͔֬ΊΔඞཁ͕ແ͍
ܕഁΓٕͦͷ ʙFOVNʙ
enum Result<T> { case Success(T) case Failure(NSError) init(_ value: T)
{ self = .Success(value) } init(_ error: NSError) { self = .Failure(error) } } enum
ෳͷঢ়ଶΛऔΓ͏Δ • Aঢ়ଶͳΒaΛ࣋ͬͯΔ͚ͲɼBঢ়ଶͰ࣋ͬͯͳ͍ • ͱΓ͋͑ͣAnyObjectʹͿͪࠐ·ͳ͍͍ͯ͘ • ͲΕ͔ͻͱͭͷঢ়ଶ͔͠औΓಘͳ͍ • A͔ͭBͳঢ়ଶଘࡏ͠ͳ͍
ͭ·ΓResultͱ • ޭ͔ࣦഊ͔ΛऔΓ͏Δ • ޭͨ͠ΒT͕ɼࣦഊͨ͠ΒNSError͕ಘΒΕΔ • ୯७ʹOptionalΛฒΔͷʹൺͯѻ͍͍͢ • ྆ํ͕͋Δ྆ํແ͍ͳΜͯ͜ͱ͋Γ͑ͳ͍
func parse(JSON: AnyObject?) -> Result<User> let JSON: AnyObject? = //
... let result = parse(JSON) switch result { case .Success(let user): // ... case .Failure(let error): // ... }
+VTUB&JUIFS
0QUJPOBM
Optionalಉྨ • OptionalͻͱͭͷgenericܕΛऔΔenum • enum Optional<T> • ͳʹ͔ͷ͕͋Δ/ͳ͍ͷ2छྨͷঢ়ଶΛऔΓ͏Δ • SomeͩͬͨΒTΛ࣋ͬͯΔ
• NoneͩͬͨΒnilΛ࣋ͬͯΔ
5IJTJTܕഁΓ
·ͱΊ
ܕৼΔ͍ͱΔ ܕͷ͋ΔϓϩάϥϜ ܕΛഁΔ
͋Γ͕ͱ͏͍͟͝·ͨ͠