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
Swift4.2で追加されたDynamic Member Lookupを使ってみよう
Search
tamappe
March 19, 2019
Technology
0
850
Swift4.2で追加されたDynamic Member Lookupを使ってみよう
try! Swift Pre Talks でお話ししました登壇資料です @tamappe
tamappe
March 19, 2019
Tweet
Share
More Decks by tamappe
See All by tamappe
async/awaitの性能をDartとSwiftとの比較で読み解く
tamappe
2
1.3k
商業雑誌に技術記事を寄稿した振り返り
tamappe
3
1.4k
リモートワークでBitriseを導入したときにハマりにハマった話
tamappe
0
240
SwiftUIとFlutterを比較する
tamappe
1
1.2k
そのAlertController 回転ロックできないってよ
tamappe
1
630
レアジョブアプリでのアクセス負荷で生じたAPI遅延問題を アプリエンジニア視点で対策を考えてみる
tamappe
0
490
Fat Storyboardをリファクタリングしてみた
tamappe
0
290
Other Decks in Technology
See All in Technology
現地でMeet Upをやる場合の注意点〜反省点を添えて〜
shotashiratori
0
520
10分でわかるfreeeのQA
freee
1
3.4k
新R25、乃木坂46 Mobileなどのファンビジネスを支えるマルチテナンシーなプラットフォームの全体像 / cam-multi-cloud
cyberagentdevelopers
PRO
1
130
使えそうで使われないCloudHSM
maikamibayashi
0
170
ABEMA のコンテンツ制作を最適化!生成 AI x クラウド映像編集システム / abema-ai-editor
cyberagentdevelopers
PRO
1
180
初心者に Vue.js を 教えるには
tsukuha
5
390
わたしとトラックポイント / TrackPoint tips
masahirokawahara
1
240
ネット広告に未来はあるか?「3rd Party Cookie廃止とPrivacy Sandboxの効果検証の裏側」 / third-party-cookie-privacy
cyberagentdevelopers
PRO
1
130
Amazon FSx for NetApp ONTAPを利用するにあたっての要件整理と設計のポイント
non97
1
160
なんで、私がAWS Heroに!? 〜社外の広い世界に一歩踏み出そう〜
minorun365
PRO
6
1.1k
AWS re:Inventを徹底的に楽しむためのTips / Tips for thoroughly enjoying AWS re:Invent
yuj1osm
1
560
マネジメント視点でのre:Invent参加 ~もしCEOがre:Inventに行ったら~
kojiasai
0
460
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
296
20k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
680
The Pragmatic Product Professional
lauravandoore
31
6.3k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.6k
Designing for humans not robots
tammielis
249
25k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
What's new in Ruby 2.0
geeforr
342
31k
KATA
mclloyd
29
13k
The Cult of Friendly URLs
andyhume
78
6k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Transcript
-FU`TVTF %ZOBNJD.FNCFS-PPLVQ JOUSPEVDFEJO4XJGU !UBNBQQF USZ4XJGU1SF5BMLT
struct Me { let name = “Yosuke Tamaoki” let age
= 31 let company = “Summer" + “Freelance“ let os = "iOS" + "Android" let hobby = "Splatoon2" + “Poker" struct Language { let swift let objective-c let java let kotlin } } "CPVU!UBNBQQF
1PXFSPGEZOBNJDNFNCFSMPPLVQ )PXUP*NQMFNFOU 4BNQMFDPEFT $PODMVTJPO 4VNNBSZPGNZUBML !EZOBNJD.FNCFS-PPLVQ QSPUPDPM\^
// Dictionary let classRoom: [String: Any] = [ "teacher": "Yamada",
“studentNum”: 30 ] // before let teacher = classRoom["teacher"] // "Yamada" let count = classRoom["studentNum"] // 30 let teacher = classRoom.teacher // compile error let count = classRoom.studentNum // compile error // after let teacher = classRoom.teacher // "Yamada" let count = classRoom.studentNum // 30 1PXFSPGEZOBNJDNFNCFSMPPLVQ
"EE!EZOBNJD.FNCFS-PPLVQ CFGPSFUIFDMBTT "EElTVCTDSJQUzEFpOJUJPO "EEMBCFMEZOBNJD.FNCFSUP UIFBSHVNFOUPGTVCTDSJQUEFpOJUJPO )PXUP*NQMFNFOU
4BNQMFDPEF $MBTT 4USVDU 1SPUPDPM &OVN &YQMBJO &YQMBJO TMJEFPOMZ TMJEFPOMZ
$MBTTDBTF @dynamicMemberLookup class User { let firstName: String let lastName:
String let age: Int init(firstName: String, lastName: String, age: Int) { self.firstName = firstName self.lastName = lastName self.age = age } subscript(dynamicMember key: String) -> Any { switch key { case "upperFirstName": return firstName.uppercased() case "upperLastName": return lastName.uppercased() case "adalt": return 30 case "children": return 15 default: return "" } } }
$MBTTDBTF @dynamicMemberLookup class User { let firstName: String let lastName:
String let age: Int init(firstName: String, lastName: String, age: Int) { self.firstName = firstName self.lastName = lastName self.age = age } subscript(dynamicMember key: String) -> Any { switch key { case "upperFirstName": return firstName.uppercased() case "upperLastName": return lastName.uppercased() case "adalt": return 30 case "children": return 15 default: return "" } } }
6TFDBTF let hanakoYamada = User(firstName: "Hanako", lastName: "Yamada", age: 20)
print(hanakoYamada.firstName) // "Hanako" print(hanakoYamada.lastName) // "Yamada" print(hanakoYamada.age) // 20 print(hanakoYamada.upperFirstName) // "HANAKO" print(hanakoYamada.upperLastName) // "YAMADA" print(hanakoYamada.adalt) // 30 print(hanakoYamada.children) // 15 print(hanakoYamada.young) // ""
TUSVDUDBTF @dynamicMemberLookup struct Person { subscript(dynamicMember name: String) -> String
{ return "Hello, \(name)" } }
TUSVDUDBTF @dynamicMemberLookup struct Person { subscript(dynamicMember name: String) -> String
{ return "Hello, \(name)" } }
6TFDBTF let person = Person() print(person.swift) // "Hello, swift” print(person.hanako)
// "Hello, hanako” print(person.taro) // "Hello, taro"
5IBU`T"MM 5IBOLZPV https://blog.tamappe.com
1SPUPDPMDBTF @dynamicMemberLookup protocol SampleProtocol { associatedtype Key associatedtype Value subscript(key:
Key) -> Value? { get } } extension SampleProtocol where Key == String { subscript(dynamicMember member: String) -> Value? { return self[member] } } extension Dictionary: SampleProtocol {} 1SPUPDPMFYUFOTJPO
6TFDBTF let hanakoDic: [String : Any] = [ "firstName": "hanako",
"lastName": "yamada", "age": 20 ] print(hanakoDic.firstName as! String) // “hanako" print(hanakoDic.age as! Int) // 20
&OVNDBTF @dynamicMemberLookup enum JSON { case number(Int) case string(String) case
array([JSON]) case dictionary([String: JSON]) subscript(dynamicMember key: String) -> JSON? { guard case .dictionary(let dict) = self else { return nil } return dict[key] } } extension JSON { var numberValue: Int? { guard case .number(let n) = self else { return nil } return n } var stringValue: String? { guard case .string(let s) = self else { return nil } return s } subscript(index: Int) -> JSON? { guard case .array(let arr) = self, arr.indices.contains(index) else { return nil } return arr[index] } subscript(key: String) -> JSON? { guard case .dictionary(let dict) = self else { return nil } return dict[key] } } +40/
&OVNDBTF @dynamicMemberLookup enum JSON { case number(Int) case string(String) case
array([JSON]) case dictionary([String: JSON]) subscript(dynamicMember key: String) -> JSON? { guard case .dictionary(let dict) = self else { return nil } return dict[key] } } +40/
&OVNDBTF extension JSON { var numberValue: Int? { guard case
.number(let n) = self else { return nil } return n } var stringValue: String? { guard case .string(let s) = self else { return nil } return s } subscript(index: Int) -> JSON? { guard case .array(let arr) = self, arr.indices.contains(index) else { return nil } return arr[index] } subscript(key: String) -> JSON? { guard case .dictionary(let dict) = self else { return nil } return dict[key] } } +40/
6TFDBTF let json = JSON.dictionary([ "firstName": .string("hanako"), "lastName": .string("Yamada"), "age":
.number(20), "language": .array([ .string(“Swift"), .string("Objective-c"), .string("Kotlin"), .string("Java"), ]) ]) if let firstName = json.firstName?.stringValue { print(firstName) // "hanako" }