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
CSC509 Lecture 12
javiergs
PRO
0
160
CSC509 Lecture 11
javiergs
PRO
0
180
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
640
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
110
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
140
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
3
1.2k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
260
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Producing Creativity
orderedlist
PRO
341
39k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Six Lessons from altMBA
skipperchong
27
3.5k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
130
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Into the Great Unknown - MozCon
thekraken
32
1.5k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Designing the Hi-DPI Web
ddemaree
280
34k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
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