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
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
hotwire_or_react
harunatsujita
8
4k
カラム追加で増えるActiveRecordのメモリサイズ イメージできますか?
asayamakk
4
1.5k
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
1
290
macOS でできる リアルタイム動画像処理
biacco42
4
1.6k
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
200
Nuxtベースの「WXT」でChrome拡張を作成する | Vue Fes 2024 ランチセッション
moshi1121
1
490
/←このスケジュール表に立ち向かう フロントエンド開発戦略 / A front-end development strategy to tackle a single-slash schedule.
nrslib
1
590
カスタムしながら理解するGraphQL Connection
yanagii
1
1.2k
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.1k
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
950
現場で役立つモデリング 超入門
masuda220
PRO
12
2.9k
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
48
10k
BBQ
matthewcrist
85
9.3k
The Art of Programming - Codeland 2020
erikaheidi
51
13k
Designing Experiences People Love
moore
138
23k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
KATA
mclloyd
29
13k
Agile that works and the tools we love
rasmusluckow
327
21k
How to Think Like a Performance Engineer
csswizardry
19
1.1k
We Have a Design System, Now What?
morganepeng
50
7.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
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