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

1年前に作った物と同じ目的のライブラリをまたSwift 3で作ってみた

fromkk
November 29, 2016

1年前に作った物と同じ目的のライブラリをまたSwift 3で作ってみた

TMValidatorを作った経験を活かしてSwift 3で同じ目的のライブラリを作ってみました。

fromkk

November 29, 2016
Tweet

More Decks by fromkk

Other Decks in Technology

Transcript

  1. TMValidatorͷαϯϓϧίʔυ UITextField *name = [[UITextField alloc] init]; name.text = @"໊લ";

    TMValidatorField *fieldName = [TMValidatorField fieldWithValue:name.text andLabel:@"name" andElement:name]; [[fieldName addRule:[TMValidatorRuleRequired rule]] addRule:[TMValidatorRuleMaxLength ruleWithLength:@10]]; TMValidator *validator = [TMValidator validator]; [[validator addField:fieldName]; [validator runWithSuccesses:^(NSArray *successes) { NSLog(@"success! %@", successes); } andFailure:^(NSArray *errors) { NSLog(@"failure! %@", errors); }]; (C) fromkk. 2016 12
  2. Valyͷαϯϓϧίʔυ let value: String? = self.textField.text let result = Valy.factory()

    .add(rule: ValyRule.required) .add(rule: ValyRule.maxLength(10)) .run(with: value) switch result { case .success: print("success!") case .failure(let rule): print("failed rule \(rule)") } (C) fromkk. 2016 14
  3. ValyͰͷܕফڈͷྫ protocol AnyValidator { var rules: [AnyValidatorRule] { get }

    static func factory() -> AnyValidator static func factory(rules: [AnyValidatorRule]) -> AnyValidatable func add(rule: AnyValidatorRule) -> AnyValidatable func add(rules: [AnyValidatorRule]) -> AnyValidatable } protocol AnyValidatable: AnyValidator { func run(with value: String?) -> ValidatorResult } Valy.factory().run(with: value) //NG! Value of type 'AnyValidator' has no member 'run' Valy.factory().add(rule: ValyRule.required).run(with: value) //OK! (C) fromkk. 2016 20