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
Property Wrapperで遊ぼう / Play with Property Wrapper
Search
Yutaro Muta
September 11, 2019
Programming
2
280
Property Wrapperで遊ぼう / Play with Property Wrapper
帰ってきた関西モバイルアプリ研究会#3
https://kanmoba.connpass.com/event/137475/
Yutaro Muta
September 11, 2019
Tweet
Share
More Decks by Yutaro Muta
See All by Yutaro Muta
Apple Vision Pro購入RTA 1泊3日弾丸ハワイツアー / RTA: Purchase Apple Vision Pro in Hawaii
yutailang0119
0
1.2k
個人開発のたのしみ / Enjoying personal development
yutailang0119
0
780
バックポートして学ぶ新APIの仕組み
yutailang0119
0
2.3k
Backport AsyncImage
yutailang0119
0
610
xcrun Essentials
yutailang0119
6
1.2k
Let's Coding SwiftUI on iPad!
yutailang0119
1
460
Back of First Screen AB test of Hatena Bookmark iOS app with Firebase AB Testing
yutailang0119
0
270
Lab of WWDC19
yutailang0119
1
480
First getting started of SwiftSyntax
yutailang0119
2
570
Other Decks in Programming
See All in Programming
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.5k
僕がつくった48個のWebサービス達
yusukebe
18
17k
Golang と Erlang
taiyow
8
1.9k
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
190
macOS でできる リアルタイム動画像処理
biacco42
6
1.7k
CSC509 Lecture 08
javiergs
PRO
0
100
Honoの来た道とこれから
yusukebe
19
3k
Vaporモードを大規模サービスに最速導入して学びを共有する
kazukishimamoto
4
4.3k
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
240
/←このスケジュール表に立ち向かう フロントエンド開発戦略 / A front-end development strategy to tackle a single-slash schedule.
nrslib
1
590
Nuxtベースの「WXT」でChrome拡張を作成する | Vue Fes 2024 ランチセッション
moshi1121
1
500
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
664
120k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Documentation Writing (for coders)
carmenintech
65
4.4k
Happy Clients
brianwarren
97
6.7k
Git: the NoSQL Database
bkeepers
PRO
425
64k
Bash Introduction
62gerente
608
210k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
A better future with KSS
kneath
238
17k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.9k
Transcript
Property WrapperͰ༡΅͏ 2019/09/11 @ؼ͖ͬͯͨؔϞόΠϧΞϓϦݚڀձ#3 Yutaro Muta @yutailang0119
• Yutaro Muta @yutailang0119 • Hatena Co., Ltd. @Kyoto •
Conference Staff • builderscon 2017, 2018, 2019 • try! Swift Tokyo 2019, 2020 • try! Swift Tokyo 2020 νέοτൢച։࢝ • https://www.tryswift.co/events/2020/tokyo/en/ Who am I ?
⚠͝ҙ • ڥ • macOS Mojave Version 10.14.6 • Xcode
11.0 GM seed (11M419c)
Property Wrapper
Property Wrapper • https://github.com/apple/swift-evolution/blob/master/proposals/0258-property- wrappers.md • ࠷ॳͷPitchProperty Delegateͱͯ͠ɺMarch 3, 2019ʹొ
• https://forums.swift.org/t/returned-for-revision-se-0258-property-delegates/24080 • Swift 5.1 ~ • ొॳSwift 5.1ʹೖΒͳ͍ͩΖ͏ͱ͍͏ҹ͕ͩͬͨɺWWDCͰApple͕ൃදͨ͠ SwiftUIʹ༻͞Ε͍ͯΔ • Swift 5.1ͷfinal branchingMarch 18, 2019
Property Wrapper • https://github.com/apple/swift-evolution/blob/master/proposals/0258-property- wrappers.md • ࠷ॳͷPitchProperty Delegateͱͯ͠ɺMarch 3, 2019ʹొ
• https://forums.swift.org/t/returned-for-revision-se-0258-property-delegates/24080 • Swift 5.1 ~ • ొॳSwift 5.1ʹೖΒͳ͍ͩΖ͏ͱ͍͏ҹ͕ͩͬͨɺWWDCͰApple͕ൃදͨ͠ SwiftUIʹ༻͞Ε͍ͯΔ • Swift 5.1ͷfinal branchingMarch 18, 2019
https://github.com/apple/swift-evolution/blob/master/ proposals/0258-property-wrappers.md https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md
https://github.com/apple/swift-evolution/blob/master/ proposals/0258-property-wrappers.md https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md
OSS SwiftͰ࣮த
࣮ঢ়گ • େͰ͖͍ͯΔ • https://github.com/apple/swift/pulls?utf8=✓&q=is%3Apr+SE-+0258 • όάใࠂ͔ͳΓ͋Δ • https://bugs.swift.org/issues/?jql=labels+%3D+PropertyWrappers
BugΛ୳ͦ͏
Property WrapperͷΑ͋͘Δྫ
Property Wrapper x UserDefaults
Property Wrapper x UserDefaults • [WWDC 2019] What's New in
Swift • https://developer.apple.com/videos/play/wwdc2019/402 • UserDefaultsΛWrapͯ͠ɺϘΠϥʔϓϨʔτΛ௵͍ͯ͘͠
@propertyWrapper struct UserDefault<T> { let key: String let defaultValue: T
init(_ key: String, defaultValue: T) { self.key = key self.defaultValue = defaultValue UserDefaults.standard.register(defaults: [key: defaultValue]) } var wrappedValue: T { get { return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue } set { UserDefaults.standard.set(newValue, forKey: key) } } } Property Wrapper x UserDefaults
@UserDefault("USES_TOUCH_ID", defaultValue: false) static var usesTouchID: Bool @UserDefault("LOGGED_IN", defaultValue: false)
static var isLoggedIn: Bool if !isLoggedIn && usesTouchID { !authenticateWithTouchID() } Property Wrapper x UserDefaults
defaultValue == nil Ͱ͖ͦ͏ͩͳ
@propertyWrapper struct UserDefault<T> { let key: String let defaultValue: T
init(_ key: String, defaultValue: T) { self.key = key self.defaultValue = defaultValue } var wrappedValue: T { get { return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue } set { UserDefaults.standard.set(newValue, forKey: key) } } } Property Wrapper x UserDefaults x Optional
@UserDefault("USES_TOUCH_ID", defaultValue: nil) static var usesTouchID: Bool @UserDefault("LOGGED_IN", defaultValue: false)
static var isLoggedIn: Bool if !isLoggedIn && usesTouchID { !authenticateWithTouchID() } Property Wrapper x UserDefaults x Optional
@UserDefault("USES_TOUCH_ID", defaultValue: nil) static var usesTouchID: Bool @UserDefault("LOGGED_IN", defaultValue: false)
static var isLoggedIn: Bool if !isLoggedIn && usesTouchID { !authenticateWithTouchID() } Property Wrapper x UserDefaults x Optional
None
@UserDefault("USES_TOUCH_ID", defaultValue: nil) static var usesTouchID: Bool print(type(of: usesTouchID)) //
Optional<Bool> ܕΛݟͯΈΔ
None
ͱ͍͏͜ͱͰ
https://bugs.swift.org/browse/SR-11443
SR-11443 Property Wrapper overwrites type declaration • bugs swiftʹग़ͨ͠
Simple Case import Foundation var value: Any = "value" @propertyWrapper
struct Wrapper<Value> { private let defaultValue: Value init(defaultValue: Value) { self.defaultValue = defaultValue } var wrappedValue: Value { get { (value as? Value) ?? defaultValue } set { value = newValue } } } final class Playground { @Wrapper(defaultValue: nil) var explicitlyOptional: String? @Wrapper(defaultValue: nil) var implicitlyOptional: String @Wrapper(defaultValue: "NonOptional") var nonOptional: String func printExplicitlyOptionalType() { print(type(of: self.explicitlyOptional)) } func printImplicitlyOptionalType() { print(type(of: self.implicitlyOptional)) } func printNonOptionalType() { print(type(of: self.nonOptional)) } } let playground = Playground() playground.printExplicitlyOptionalType() // Optional<String> playground.printImplicitlyOptionalType() // Optional<String> playground.printNonOptionalType() // String
·ͱΊ
·ͱΊ • Property WrapperΛ͍ͯ͠ɺόάใࠂΛ͍ͯ͜͠͏
&OKPZ1SPQFSUZ8SBQQFS 5IBOLT w NVUBZVUBSP!HNBJMDPN w IUUQTUXJUUFSDPNZVUBJMBOH w IUUQTHJUIVCDPNZVUBJMBOH