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

Mockable UserDefaults with Duck typing

Mockable UserDefaults with Duck typing

417.72KI

June 18, 2019
Tweet

More Decks by 417.72KI

Other Decks in Programming

Transcript

  1. struct Me { let name = "Takuhiro Muta" let aka

    = "417.72KI" let experienceYears = 5 let company = "iRidge inc." let twitter = "417_72ki" let qiita = "417_72ki" let gitHub = "417-72KI" let products = [ "BuildConfig.swift", "MockUserDefaults", ] } TFMGEFTDSJQUJPO
  2. %VDL5ZQJOH w 0OFPGUIFNFUIPETUPJNQMFNFOU QPMZNPSQIJTN w 6TFEJOEZOBNJDMBOHVBHFT FH3VCZ 1ZUIPO  w

    0CKFDUTTVJUBCJMJUZJTEFUFSNJOFECZUIF QSFTFODFPGDFSUBJONFUIPETBOE QSPQFSUJFT w /PUPCKFDU`TPXOUZQF
  3. %VDL5ZQJOH class Duck: def walk(self): print("Duck walking") def quack(self): print("Quack!!")

    class Dog: def walk(self): print("Dog walking") def bark(self): print("Bark!!") def walk_and_quack(animal): animal.walk() animal.quack()
  4. %VDL5ZQJOH class Duck: def walk(self): print("Duck walking") def quack(self): print("Quack!!")

    class Dog: def walk(self): print("Dog walking") def bark(self): print("Bark!!") def walk_and_quack(animal): animal.walk() animal.quack()
  5. %VDL5ZQJOH class Duck: def walk(self): print("Duck walking") def quack(self): print("Quack!!")

    class Dog: def walk(self): print("Dog walking") def bark(self): print("Bark!!") def walk_and_quack(animal): animal.walk() animal.quack()
  6. %VDL5ZQJOH class Duck: def walk(self): print("Duck walking") def quack(self): print("Quack!!")

    class Dog: def walk(self): print("Dog walking") def bark(self): print("Bark!!") def walk_and_quack(animal): animal.walk() animal.quack()
  7. %VDL5ZQJOH walk_and_quack(Duck()) # Duck walking # Quack!! walk_and_quack(Dog()) # Dog

    walking # AttributeError: 'Dog' object has # no attribute 'quack'
  8. *O0CK$ @interface Duck: NSObject - (void)walk; - (void)quack; @end @implementation

    Duck - (void)walk { NSLog(@"Duck walking"); } - (void)quack { NSLog(@"Quack!!"); } @end void walkAndQuack(id animal) { [animal walk]; [animal quack]; }
  9. *O0CK$ @interface Duck: NSObject - (void)walk; - (void)quack; @end @implementation

    Duck - (void)walk { NSLog(@"Duck walking"); } - (void)quack { NSLog(@"Quack!!"); } @end void walkAndQuack(id animal) { [animal walk]; [animal quack]; }
  10. *O0CK$ @interface Duck: NSObject - (void)walk; - (void)quack; @end @implementation

    Duck - (void)walk { NSLog(@"Duck walking"); } - (void)quack { NSLog(@"Quack!!"); } @end void walkAndQuack(id animal) { [animal walk]; [animal quack]; }
  11. *O0CK$ walkAndQuack([Duck new]); // Duck walking // Quack!! walkAndQuack([Dog new]);

    // Dog walking // unrecognized selector sent to instance