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
2.3k
5
Share
Types in Swift
Cocoa勉強会関西 #60の発表資料です
yashigani
February 28, 2015
More Decks by yashigani
See All by yashigani
Hatena Engineer Seminar #10
yashigani
0
2.4k
Dependency Injection in iOS Apps
yashigani
2
520
Multiple layout with UITableViewCell
yashigani
1
3.6k
Lazy stored property
yashigani
3
2.2k
Code reviewing in iOS app development
yashigani
9
4.6k
Useful 3 techniques developing in iOS Apps
yashigani
0
1.6k
Introduction to WebKitPlus
yashigani
2
2.5k
Swift with apps by Hatena
yashigani
13
30k
What's new WebKit.framework
yashigani
2
1.5k
Other Decks in Programming
See All in Programming
NakouPAY説明用
annouim0
0
240
ハーネスエンジニアリングにどう向き合うか 〜ルールファイルを超えて開発プロセスを設計する〜 / How to approach harness engineering
rkaga
24
13k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyKaigi 2026, Hakodate, Japan
marcoroth
0
150
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
270
AI-DLC Deep Dive
yuukiyo
9
4.4k
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
150
Kingdom of the Machine
yui_knk
2
380
Liberating Ruby's Parser from Lexer Hacks
ydah
2
1.4k
「Linuxサーバー構築標準教科書」を読んでみた #ツナギメオフライン.7
akase244
0
1.4k
Server-Side Kotlin LT大会 vol.18 [Kotlin-lspの最新情報と Neovimのlsp設定例]
yasunori0418
1
160
JOAI2026 1st solution - heron0519 -
heron0519
0
140
〜バイブコーディングを超えて〜 チームで実験し続けたAI駆動開発
tigertora7571
0
120
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
How to Talk to Developers About Accessibility
jct
2
180
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
110
Ethics towards AI in product and experience design
skipperchong
2
260
A Tale of Four Properties
chriscoyier
163
24k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.4k
The Language of Interfaces
destraynor
162
26k
Into the Great Unknown - MozCon
thekraken
41
2.4k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
120
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
64
55k
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ܕഁΓ
·ͱΊ
ܕৼΔ͍ͱΔ ܕͷ͋ΔϓϩάϥϜ ܕΛഁΔ
͋Γ͕ͱ͏͍͟͝·ͨ͠