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
790
バックポートして学ぶ新APIの仕組み
yutailang0119
0
2.4k
Backport AsyncImage
yutailang0119
0
620
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
ヤプリ新卒SREの オンボーディング
masaki12
0
130
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
Jakarta EE meets AI
ivargrimstad
0
590
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Jakarta EE meets AI
ivargrimstad
0
120
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
250
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
693
190k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
For a Future-Friendly Web
brad_frost
175
9.4k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Bash Introduction
62gerente
608
210k
Embracing the Ebb and Flow
colly
84
4.5k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Why Our Code Smells
bkeepers
PRO
334
57k
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