$30 off During Our Annual Pro Sale. View Details »

Property Wrappersがもたらす新しいSwiftプログラミング / New Swift programming with Property Wrappers

darquro
September 06, 2019

Property Wrappersがもたらす新しいSwiftプログラミング / New Swift programming with Property Wrappers

iOSDC Japan 2019 day1(2019/8/6) Track A LT 16:15

darquro

September 06, 2019
Tweet

More Decks by darquro

Other Decks in Technology

Transcript

  1. Re:valua t ion https://fril.jp/ L ead & Delight User F

    irst Ownership Fail Smart 1SPQFSUZ8SBQQFST͕΋ͨΒ͢ ৽͍͠4XJGUϓϩάϥϛϯά !J04%$+BQBO
  2. J04&OHJOFFS 3BLVUFO JOD ϥΫϚ    "CPVU.F EBSRVSP :VLJ,VSPEB

    !EBSRVSP !EBSRVSP
  3. 1SPQFSUZ8SBQQFST  

  4.   1SPQFSUZ8SBQQFST w "WBJMBCMF4XJGU w ϓϩύςΟΞΫηεʹԠͯ͡ڍಈΛXSBQ w ϓϩύςΟΠϯελϯε͝ͱʹσʔλΛอ࣋Մೳ w

    ॳظ஋஗Ԇ࣮ߦ
  5.   6TFS%FGBVMUT&YBNQMF @propertyWrapper struct UserDefault<T> { var wrappedValue: T

    { get { return UserDefaults.standard .object(forKey: key) as? T ?? defaultValue } set { UserDefaults.standard.set(newValue, forKey: key) } } }
  6.   6TFS%FGBVMUT&YBNQMF @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 { … } }
  7.   6TFS%FGBVMUT&YBNQMF struct Foo { @UserDefault("IS_LOGGED_IN", defaultValue: false) static

    var isLoggedIn: Bool } Foo.isLoggedIn = true // UserDefaults.standard.object(forKey: "IS_LOGGED_IN") -> 1 Foo.isLoggedIn = false // UserDefaults.standard.object(forKey: "IS_LOGGED_IN") -> 0
  8.   6TFS%FGBVMUT&YBNQMF

  9.   4UBUF&YBNQMF struct LandmarkList: View { @State var showFavoriteOnly

    = true var body: some View { NavigationView { List { Toggle(isOn: $showFavoriteOnly) { Text("Favorite Only") } … } … }
  10.   4UBUF&YBNQMF @propertyWrapper public struct State<Value> : DynamicProperty, BindingConvertible

    { …… /// The current state value. public var wrappedValue: Value { get nonmutating set } …… /// Produces the binding referencing this state value public var projectedValue: Binding<Value> { get } }
  11.   7BMJEBUJPO&YBNQMF @propertyWrapper struct Validate<Value> { var wrappedValue: Value?

    var isValid: Bool { guard let value = wrappedValue else { return false } return validateion(value) } private let validateion: (Value) -> Bool private let messageClosure: (Value) -> String init(_ validation: @escaping (Value) -> Bool, message messageClosure: @escaping (Value) -> String) { self.validateion = validation self.messageClosure = messageClosure } }
  12.   7BMJEBUJPO&YBNQMF enum StringValidationType { case email case phoneNumber

    var regex: String { switch self { case .email: return #"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za- z]{2,64}"# case .phoneNumber: return "^[0-9]{6,14}$" } } }
  13.   7BMJEBUJPO&YBNQMF extension Validate where Value == String {

    init(_ type: StringValidationType) { self.init({ $0.range(of: type.regex, options: .regularExpression) != nil }, message: { "Error:\($0) does not match email" }) } }
  14.   7BMJEBUJPO&YBNQMF struct Foo { @Validate(.email) var email: String?

    @Validate(.phoneNumber) var phoneNumber: String? var isValidAll: Bool { _email.isValid && _phoneNumber.isValid } } var foo = Foo() foo.email = "abc@example.com" foo.phoneNumber = "09012341234" foo.isValidAll // -> "true"
  15. ந৅Խ  

  16.   ந৅Խ w ܧঝ w 1SPUPDPM w &YUFOTJPOT w

    1SPQFSUZ8SBQQFST
  17.   ந৅Խ 1SPQFSZ8SBQQFST ࣮૷ίετ ந৅౓ 1SPUPDPM &YUFOTJPOT ܧঝ

  18.   ల๬ ΞʔΩςΫνϟ ϑϨʔϜϫʔΫ σβΠϯύλʔϯ 044ϥΠϒϥϦ

  19. ·ͱΊ  

  20.   ·ͱΊ 1SPQFSUZ8SBQQFST w ΑΓந৅తʹػೳΛϓϩύςΟͷΈʹఏڙͰ͖Δ w ϘΠϥʔϓϨʔτͷলུʹͳΔ w !YYY@YYYYYYͱ͍ͬͨಠࣗͷγϯλοΫε

  21.   4XJGU&WPMVUJPO1SPQPTBM IUUQTHJUIVCDPNBQQMFTXJGUFWPMVUJPOCMPCNBTUFSQSPQPTBMTQSPQFSUZXSBQQFSTNE

  22. 5IBOLZPV