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. .PDLBCMF6TFS%FGBVMUT
    XJUI
    %VDLUZQJOH

    QPUBUPUJQT

    View Slide

  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

    View Slide

  3. %VDL5ZQJOH

    View Slide

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

    w 0CKFDUTTVJUBCJMJUZJTEFUFSNJOFECZUIF
    QSFTFODFPGDFSUBJONFUIPETBOE
    QSPQFSUJFT
    w /PUPCKFDU`TPXOUZQF

    View Slide

  5. r%BWF5IPNBT
    l*GJUXBMLTMJLFBEVDLBOERVBDLTMJLFB
    EVDL JUNVTUCFBEVDLz

    View Slide

  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()

    View Slide

  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()

    View Slide

  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()

    View Slide

  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()

    View Slide

  10. %VDL5ZQJOH
    walk_and_quack(Duck())
    # Duck walking
    # Quack!!
    walk_and_quack(Dog())
    # Dog walking
    # AttributeError: 'Dog' object has
    # no attribute 'quack'


    View Slide

  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];
    }

    View Slide

  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];
    }

    View Slide

  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];
    }

    View Slide

  14. *O0CK$
    walkAndQuack([Duck new]);
    // Duck walking
    // Quack!!
    walkAndQuack([Dog new]);
    // Dog walking
    // unrecognized selector sent to instance


    View Slide

  15. )PXUPNPDL
    6TFS%FGBVMUT

    View Slide

  16. r%BWF5IPNBT
    l*GJUXBMLTMJLFBEVDLBOERVBDLTMJLFB
    EVDL JUNVTUCFBEVDLz

    View Slide

  17. l*GJUTBWFTPCKFDUTMJLF6TFS%FGBVMT

    BOEMPBETPCKFDUTMJLF6TFS%FGBVMUT 

    JUNVTUCF6TFS%FGBVMUTz

    View Slide

  18. %FpOFBMMNFUIPETJO
    /46TFS%FGBVMUT

    View Slide

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

    View Slide

  20. *NQMFNFOU

    View Slide

  21. %FpOFFYUFOTJPOUPVTFJU
    BT/46TFS%FGBVMUT

    View Slide

  22. %FpOFFYUFOTJPOUPVTFJU
    BT/46TFS%FGBVMUT

    View Slide

  23. %FpOFFYUFOTJPOUPVTFJU
    BT/46TFS%FGBVMUT

    View Slide

  24. View Slide

  25. 5IBOLZPV
    IUUQTHJUIVCDPN,*.PDL6TFS%FGBVMUT

    View Slide