Mockable UserDefaults with Duck typing

Mockable UserDefaults with Duck typing

7a78f23eef1b0e883ef44c229a54f0bb?s=128

417.72KI

June 18, 2019
Tweet

Transcript

  1. 2.

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

    %VDL5ZQJOH w 0OFPGUIFNFUIPETUPJNQMFNFOU QPMZNPSQIJTN w 6TFEJOEZOBNJDMBOHVBHFT FH3VCZ 1ZUIPO  w

    0CKFDUTTVJUBCJMJUZJTEFUFSNJOFECZUIF QSFTFODFPGDFSUBJONFUIPETBOE QSPQFSUJFT w /PUPCKFDU`TPXOUZQF
  3. 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()
  4. 7.

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

    %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. 9.

    %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. 10.

    %VDL5ZQJOH walk_and_quack(Duck()) # Duck walking # Quack!! walk_and_quack(Dog()) # Dog

    walking # AttributeError: 'Dog' object has # no attribute 'quack'
  8. 11.

    *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. 12.

    *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. 13.

    *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. 14.

    *O0CK$ walkAndQuack([Duck new]); // Duck walking // Quack!! walkAndQuack([Dog new]);

    // Dog walking // unrecognized selector sent to instance
  12. 20.
  13. 24.