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

Property Wrapperで遊ぼう / Play with Property Wrapper

Yutaro Muta
September 11, 2019

Property Wrapperで遊ぼう / Play with Property Wrapper

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

Yutaro Muta

September 11, 2019
Tweet

More Decks by Yutaro Muta

Other Decks in Programming

Transcript

  1. • 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 ?
  2. 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
  3. 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
  4. Property Wrapper x UserDefaults • [WWDC 2019] What's New in

    Swift • https://developer.apple.com/videos/play/wwdc2019/402 • UserDefaultsΛWrapͯ͠ɺϘΠϥʔϓϨʔτΛ௵͍ͯ͘͠
  5. @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
  6. @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
  7. @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
  8. @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
  9. @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
  10. 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