Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Property Wrapperで遊ぼう / Play with Property Wrapper

D36d6bab8f1e0ff4bb3377571e5f7dcd?s=47 Yutaro Muta
September 11, 2019

Property Wrapperで遊ぼう / Play with Property Wrapper

帰ってきた関西モバイルアプリ研究会#3 https://kanmoba.connpass.com/event/137475/

D36d6bab8f1e0ff4bb3377571e5f7dcd?s=128

Yutaro Muta

September 11, 2019
Tweet

Transcript

  1. Property WrapperͰ༡΅͏ 2019/09/11 @ؼ͖ͬͯͨؔ੢ϞόΠϧΞϓϦݚڀձ#3 Yutaro Muta @yutailang0119

  2. • 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 ?
  3. ⚠͝஫ҙ • ؀ڥ • macOS Mojave Version 10.14.6 • Xcode

    11.0 GM seed (11M419c)
  4. Property Wrapper

  5. Property Wrapper • https://github.com/apple/swift-evolution/blob/master/proposals/0258-property- wrappers.md • ࠷ॳͷPitch͸Property 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 branching͸March 18, 2019
  6. Property Wrapper • https://github.com/apple/swift-evolution/blob/master/proposals/0258-property- wrappers.md • ࠷ॳͷPitch͸Property 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 branching͸March 18, 2019
  7. 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

  8. 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

  9. OSS SwiftͰ͸࣮૷த

  10. ࣮૷ঢ়گ • େ࿮͸Ͱ͖͍ͯΔ • https://github.com/apple/swift/pulls?utf8=✓&q=is%3Apr+SE-+0258 • όάใࠂ΋͔ͳΓ͋Δ • https://bugs.swift.org/issues/?jql=labels+%3D+PropertyWrappers

  11. BugΛ୳ͦ͏

  12. Property WrapperͷΑ͋͘Δྫ

  13. Property Wrapper x UserDefaults

  14. Property Wrapper x UserDefaults • [WWDC 2019] What's New in

    Swift • https://developer.apple.com/videos/play/wwdc2019/402 • UserDefaultsΛWrapͯ͠ɺϘΠϥʔϓϨʔτΛ௵͍ͯ͘͠
  15. @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
  16. @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
  17. defaultValue == nil ΋Ͱ͖ͦ͏ͩͳ

  18. @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
  19. @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
  20. @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
  21. None
  22. @UserDefault("USES_TOUCH_ID", defaultValue: nil) static var usesTouchID: Bool print(type(of: usesTouchID)) //

    Optional<Bool> ܕΛݟͯΈΔ
  23. None
  24. ͱ͍͏͜ͱͰ

  25. https://bugs.swift.org/browse/SR-11443

  26. SR-11443 Property Wrapper overwrites type declaration • bugs swiftʹग़ͨ͠

  27. 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
  28. ·ͱΊ

  29. ·ͱΊ • Property WrapperΛ࢖͍౗ͯ͠ɺόάใࠂΛ͍ͯ͜͠͏

  30. &OKPZ1SPQFSUZ8SBQQFS 5IBOLT w NVUBZVUBSP!HNBJMDPN w IUUQTUXJUUFSDPNZVUBJMBOH w IUUQTHJUIVCDPNZVUBJMBOH