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
470
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
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
130
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
250
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
140
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
180
HTML/CSS超絶浅い説明
yuki0329
0
190
カンファレンス動画鑑賞会のススメ / Osaka.swift #1
hironytic
0
170
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
210
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
370
ESLintプラグインを使用してCDKのセオリーを適用する
yamanashi_ren01
2
240
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
390
functionalなアプローチで動的要素を排除する
ryopeko
1
210
情報漏洩させないための設計
kubotak
5
1.3k
Featured
See All Featured
Embracing the Ebb and Flow
colly
84
4.5k
Mobile First: as difficult as doing things right
swwweet
222
9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
98
18k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
173
51k
A designer walks into a library…
pauljervisheath
205
24k
Typedesign – Prime Four
hannesfritz
40
2.5k
Docker and Python
trallard
43
3.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
230
52k
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ܕഁΓ
·ͱΊ
ܕৼΔ͍ͱΔ ܕͷ͋ΔϓϩάϥϜ ܕΛഁΔ
͋Γ͕ͱ͏͍͟͝·ͨ͠