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
Vue SFCのtemplateでTypeScriptの型を活用しよう
tsukkee
3
1.5k
hotwire_or_react
harunatsujita
8
4k
Nuxtベースの「WXT」でChrome拡張を作成する | Vue Fes 2024 ランチセッション
moshi1121
1
510
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
0
150
Piniaの現状と今後
waka292
5
1.5k
EventSourcingの理想と現実
wenas
6
2.1k
Server Driven Compose With Firebase
skydoves
0
400
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
cXML という電子商取引の トランザクションを支える プロトコルと向きあっている話
phigasui
3
2.3k
現場で役立つモデリング 超入門
masuda220
PRO
12
2.9k
レガシーな Android アプリのリアーキテクチャ戦略
oidy
1
170
CSC305 Lecture 13
javiergs
PRO
0
130
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
65
11k
Code Review Best Practice
trishagee
64
17k
Making Projects Easy
brettharned
115
5.9k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
231
17k
Agile that works and the tools we love
rasmusluckow
327
21k
Unsuck your backbone
ammeep
668
57k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
For a Future-Friendly Web
brad_frost
175
9.4k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Gamification - CAS2011
davidbonilla
80
5k
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ܕഁΓ
·ͱΊ
ܕৼΔ͍ͱΔ ܕͷ͋ΔϓϩάϥϜ ܕΛഁΔ
͋Γ͕ͱ͏͍͟͝·ͨ͠