Mockable UserDefaults with Duck typing

Mockable UserDefaults with Duck typing

7a78f23eef1b0e883ef44c229a54f0bb?s=128

417.72KI

June 18, 2019
Tweet

Transcript

  1. .PDLBCMF6TFS%FGBVMUT XJUI %VDLUZQJOH  QPUBUPUJQT

  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
  3. %VDL5ZQJOH

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

    0CKFDUTTVJUBCJMJUZJTEFUFSNJOFECZUIF QSFTFODFPGDFSUBJONFUIPETBOE QSPQFSUJFT w /PUPCKFDU`TPXOUZQF
  5. r%BWF5IPNBT l*GJUXBMLTMJLFBEVDLBOERVBDLTMJLFB EVDL JUNVTUCFBEVDLz

  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 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()
  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()
  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()
  10. %VDL5ZQJOH walk_and_quack(Duck()) # Duck walking # Quack!! walk_and_quack(Dog()) # Dog

    walking # AttributeError: 'Dog' object has # no attribute 'quack'
  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]; }
  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]; }
  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]; }
  14. *O0CK$ walkAndQuack([Duck new]); // Duck walking // Quack!! walkAndQuack([Dog new]);

    // Dog walking // unrecognized selector sent to instance
  15. )PXUPNPDL 6TFS%FGBVMUT

  16. r%BWF5IPNBT l*GJUXBMLTMJLFBEVDLBOERVBDLTMJLFB EVDL JUNVTUCFBEVDLz

  17. l*GJUTBWFTPCKFDUTMJLF6TFS%FGBVMT
 BOEMPBETPCKFDUTMJLF6TFS%FGBVMUT 
 JUNVTUCF6TFS%FGBVMUTz

  18. %FpOFBMMNFUIPETJO /46TFS%FGBVMUT

  19. %FpOFBMMNFUIPETJO /46TFS%FGBVMUT /PUFYUFOE/46TFS%FGBVMUT

  20. *NQMFNFOU

  21. %FpOFFYUFOTJPOUPVTFJU BT/46TFS%FGBVMUT

  22. %FpOFFYUFOTJPOUPVTFJU BT/46TFS%FGBVMUT

  23. %FpOFFYUFOTJPOUPVTFJU BT/46TFS%FGBVMUT ⁉

  24. None
  25. 5IBOLZPV IUUQTHJUIVCDPN,*.PDL6TFS%FGBVMUT