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
42 best practices for Symfony, a decade later
tucksaun
1
180
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
200
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
280
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
790
Zoneless Testing
rainerhahnekamp
0
120
良いユニットテストを書こう
mototakatsu
8
2.5k
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
Featured
See All Featured
How GitHub (no longer) Works
holman
311
140k
Into the Great Unknown - MozCon
thekraken
33
1.5k
The Language of Interfaces
destraynor
154
24k
We Have a Design System, Now What?
morganepeng
51
7.3k
Building an army of robots
kneath
302
44k
Why Our Code Smells
bkeepers
PRO
335
57k
Thoughts on Productivity
jonyablonski
67
4.4k
A designer walks into a library…
pauljervisheath
204
24k
Designing for humans not robots
tammielis
250
25k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
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ܕഁΓ
·ͱΊ
ܕৼΔ͍ͱΔ ܕͷ͋ΔϓϩάϥϜ ܕΛഁΔ
͋Γ͕ͱ͏͍͟͝·ͨ͠