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
290
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.3k
個人開発のたのしみ / Enjoying personal development
yutailang0119
0
830
バックポートして学ぶ新APIの仕組み
yutailang0119
0
2.4k
Backport AsyncImage
yutailang0119
0
630
xcrun Essentials
yutailang0119
6
1.2k
Let's Coding SwiftUI on iPad!
yutailang0119
1
470
Back of First Screen AB test of Hatena Bookmark iOS app with Firebase AB Testing
yutailang0119
0
280
Lab of WWDC19
yutailang0119
1
490
First getting started of SwiftSyntax
yutailang0119
2
580
Other Decks in Programming
See All in Programming
テストコード書いてみませんか?
onopon
2
210
fs2-io を試してたらバグを見つけて直した話
chencmd
0
240
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
ドメインイベント増えすぎ問題
h0r15h0
2
430
快速入門可觀測性
blueswen
0
410
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
150
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
180
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
330
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
190
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
160
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
1
270
Featured
See All Featured
Visualization
eitanlees
146
15k
BBQ
matthewcrist
85
9.4k
The Cult of Friendly URLs
andyhume
78
6.1k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
A designer walks into a library…
pauljervisheath
205
24k
Code Reviewing Like a Champion
maltzj
521
39k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
1
110
A Tale of Four Properties
chriscoyier
157
23k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Designing Experiences People Love
moore
138
23k
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