Swiftにわか勢な自分がMacアプリを作ってRxSwiftを使えるようになった話

7d1521d950d9bc5697868830cb04ff2f?s=47 yimajo
April 20, 2016

 Swiftにわか勢な自分がMacアプリを作ってRxSwiftを使えるようになった話

7d1521d950d9bc5697868830cb04ff2f?s=128

yimajo

April 20, 2016
Tweet

Transcript

  1. QPUBUPUJQT !ZJNBKP 4XJGUʹΘ͔੎ͳࣗ෼͕ .BDΞϓϦΛ࡞ͬͯ3Y4XJGUΛ ࢖͑ΔΑ͏ʹͳͬͨ࿩

  2. <         

    > ͜ͷ"SSBZͷҙຯ Θ͔Γ·͔͢ʁ
  3. -ਖ਼ղ͸- ࢲ͕potatotipsͰൃදͨ͠ճͷ൪߸ΛArrayʹ͚ͨͩ͠ <       

      > ͜ͷ"SSBZͷҙຯ Θ͔Γ·͔͢ʁ
  4. ࣗݾ঺հ w !ZJNBKP w QPUBUPUJQTճ໨ͷൃද w Έͳ͞Μొ࿥ͯ͠Δͱ͸ࢥ͏Μ Ͱ͕͢TUBSH[SOFUͱ͍͏αΠ τΛӡӦ͍ͯ͠·͢ w

    ΠϯλϏϡʔ͞Εͯ΋ྑ͍Αͱ ͍͏ํ͕͍Ε͹ڭ͑ͯԼ͍͞
  5. ϦΫϧʔτ͞Μͷ5FDI-BC1"",ͱ͍͏ ແྉͷίϫʔΩϯάεϖʔεΛ͓आΓͯ͠ίʔυॻ͍͍ͯ·͢

  6. ࿩͢͜ͱ w 3Y4XJGUͰ௒࣮༻తͳ.BDΞϓϦΛ࡞ͬͨͷͰॱ ংཱͯͯͦͷ࿩Λ͠·͢

  7. લఏ

  8. w 3Yʹ͍ͭͯ஌Βͳ͍ਓ΋෼͔Δײ͡ͷઆ໌Λ͠ ·͢

  9. w ؒҧ͍΋͋Δͱࢥ͏ͷͰ۩ମతʹಥͬࠐΜͰ΋ Βͬͨ΄͏͕ಘΔ΋ͷ͕ଟ͍Ͱ͢

  10. w 3Y$PDPB $PDPB&YUFOTJPOT ͷ࿩΋3Y4XJGUͱ ͯ͠࿩͠·͢

  11. 3Y4XJGUͰ࡞ͬͨ.BD ΞϓϦͱ͸ʁ

  12. %&.0 QPUBUPUJQTͰπΠʔτ͢Δͱಈ͍͍ͯΔ͸ͣ ಈ͍ͯͳ͔ͬͨΒଈൃදऴྃ ʢϒϩά࿮తʹ͜ͷࣸਅΛࡱ͓ͬͯ͘ͱө͑·͢ʣ

  13. εΫϦʔϯγϣοτ

  14. ͜ͷΞϓϦͷಛ௃ͱ 3Y4XJGUΛ࢖࣮ͬͨ૷

  15. ಛ௃ ൃද࣌͸ϚϧνσΟεϓϨΠͳͷͰ ࣗಈݕग़ͯ͠දࣔ͢Δ΋ͷΛબͿ σΟεϓϨΠݕ஌ͰSY@OPUJpDBUJPO

  16. SY@OPUJpDBUJPO w /4/PUJpDBUJPOͷSY@OPUJpDBUJPO࢖͏ͱΫϩʔ δϟʹ௨஌࣌Λड͚ͨࡍͷॲཧॻ͚Δ w EJTQPTF#BHΛ࢖͑Δ NSNotificationCenter.defaultCenter() .rx_notification(NSApplicationDidChangeScreenParametersNotification) .subscribeNext {

    [unowned self] _ in self.tableView.reloadData() } .addDisposableTo(disposeBag)
  17. ಛ௃ ݕࡧ͍ͨ͠ϋογϡλάΛೖྗͯ͠ ࣮ߦ SY@UFYU΍SY@UBQ

  18. SY@UFYU w 3Y4XJGUͷجૅతͳ࢖͍ํ௨ΓWBMJEBUF w ݕࡧจࣈ͕͋Ε͹ݕࡧϘλϯΛ׆ੑԽ let searchValid = searchField.rx_text .map{

    text -> Bool in 0 < text.characters.count } searchValid .bindTo(searchButton.rx_enabled) .addDisposableTo(disposeBag)
  19. SY@UBQ w 3Y4XJGUͷجૅతͳ࢖͍ํ௨ΓTVCTDSJCF/FYU w ͚Ͳ࣮͸େͨ͜͠ͱ͠ͳ͍ͳΒ!*#"DUJPOͰ͍͍ʢͳΜͰ΋SYͱ͍ ͏ͷ͸όϥϯε͕ѱ͍൑அͩͱࢥ͏ʣ searchButton.rx_tap .subscribeNext { [unowned

    self] in // ͜͜ʹtap͞Εͨॲཧॻ͘ } .addDisposableTo(disposeBag)
  20. ಛ௃ πΠʔτΛӈ͔Βࠨʹ 8FC"1*ͷ݁ՌΛ1VCMJTI4VCKFDU

  21. 5XJUUFS"1*ͷ݁ՌΛ1VCMJTI4VCKFDU w πΠʔτ͕ݟ͔ͭͬͨΒPO/FYU w "1*ͷΤϥʔͳΒPO&SSPS w ͦΕΛ4VCTDSJCFͯ͠7JFXΛ࡞Δ

  22. ಛ௃ ΞϓϦͷঢ়ଶ΋πΠʔτͷΑ͏ʹը ໘ʹදࣔͯ͋͛͠Δ 0CTFSWBCMFΛJOUFSWBMͰఆظతʹ

  23. JOUFSWBMͰఆظతʹ0CTFSWBCMFͳ΋ͷΛ w ඵ͝ͱʹ"OOPVODF͍ͬͯ͏4USVDUΛ࡞Δ return Observable<Int> .interval(60.0, scheduler: MainScheduler.instance) .map {

    _ in return Announce(id: NSUUID().UUIDString, text: announceText) }
  24. ͭͷετϦʔϜΛ߹੒͢Δ ಛ௃ ʮπΠʔτʯ΋ʮΞϓϦ͔Βͷ৘ใ௨஌ʯ΋ σΟεϓϨΠӈ͔Βࠨʹྲྀ͢

  25. NFSHF 5XJUUFS"1*઒ 1VCMJTI4VCKFDU$PNNFOU5ZQF ΞϓϦͷϝοηʔδ઒ 0CTFSWBCMF$PNNFOU5ZQF $PNNFOU5ZQFͱ͍͏ϓϩτίϧͭ͘Δ &MFNFOU͕ಉͩ͡ͱNFSHFͰ͖ΔͷͰ

  26. // ϓϩτίϧ protocol CommentType { func type() -> Comment func

    identifier() -> String func message() -> String func imageURL() -> NSURL? } enum Comment { case Tweet case Announce } // TweetΛJSON͔ΒΤϯςΟςΟԽ // HimotokiͷDecodableΛܧঝ struct Tweet: Decodable { //…লུ… } // ϓϩτίϧͷ࣮૷෦ extension Tweet: CommentType { //…লུ… } // ΞϓϦ͔Βͷϝοηʔδ৘ใ struct Announce: CommentType { //…লུ… }
  27. ౧ϓϩτίϧʹै͍ͬͯ͑͞Ε͹͍͍

  28. Ͱ ͜ͷ.BDΞϓϦ͍ͭ ϦϦʔε͢ΔΜͰ͢ʁ

  29. ͓ߴ͍ΜͰ͠ΐ͏

  30. ʜϦϦʔε͸͠ͳ͍ʂʂ ʢ.BDͷσϕϩούϥΠηϯε͸ ࣋ͬͯͳ͍͠ʣ

  31. ίʔυ͸ެ։ͯ͠·͢ IUUQTHJUIVCDPNZJNBKP.FFUVQ5XFFU ˒Λ͚ͭͯ๨Εͳ͍Α͏ʹ͓ͯ͘͠ͱྑ͍Ͱ͢Ͷ

  32. ࣍ʹ ެ։͞ΕͨίʔυΛར༻͢ΔͨΊͷ࿩ IUUQTHJUIVCDPNZJNBKP.FFUVQ5XFFU

  33. 5XJUUFSͷ"1*,FZͱ 4FDSFU͕ඞཁͰ͢ IUUQTHJUIVCDPNZJNBKP.FFUVQ5XFFU

  34. 5XFFU4FBSDI 7JFX$POUSPMMFS $PNNFOU'MPX 1SFTFOUFS w 6*͔ΒͷΠϕϯτΛॲཧ w ݕࡧ͢ΔจݴΛೖྗ w දࣔ͢Δը໘ΛબͿ

    w 5XFFU4USFBN"1*ͷ 6TF$BTFΛݺͼग़͢ w "OOPVODF6TF$BTFΛݺ ͼग़͢ w NFSHF͠TVCTDSJCF w 7JFXʹͯ͠ը໘্ʹྲྀ͢ 5XJUUFS4USFBN"1* 6TF$BTF w 5XJUUFS4USFBN"1*Λݺ ͼग़͠1VCMJTI4VCKFDU "OOPVODF 6TF$BTF w ΞϓϦ͕ࣗಈͰදࣔ͢Δ ίϝϯτΛ0CTFSWBCMF ʹ 1SFTFOUBUJPO૚ %PNBJO૚ *OGSB૚ 5XJUUFS"1* )JNPUPLJ 5XFFUදࣔ͸ϨΠϠʔυ෩ΞʔΩςΫνϟ ࢀߟ IUUQTUFDISFDSVJUNQDPKQNPCJMFQPTU w )JNPUPLJ͸͍͍ͧ w 4USFBN"1* Λݺͼग़ͤΔ
  35. ·ͱΊ

  36. खΛಈ͔͞ͳ͍ͱ3Y4XJGUΛशಘ͢Δͷ͸ ೉͍͠ ʮNFSHFͬͯҙຯෆ໌͚ͩͬͨͲ͜͜Ͱ࢖͏ͷ͔ʯ

  37. ͔͠͠࢓ࣄͰѻ͍ͬͯΔϓϩμΫγϣϯʹ ͍͖ͳΓೖΕΔͷ΋೉͍͠ 3YʹϩοΫΠϯͯ͠͠·͏ϦεΫ

  38. 3Y4XJGU࢖ͬͨ J1IPOFΞϓϦΛ࡞Ζ͏ͱࢥͬͯ΋ཉ͍͠΋ͷ ͸͢Ͱʹ͋ͬͨΓ͢Δ

  39. ͩͬͨΒ .BDΞϓϦ࡞Γ·͠ΐ͏ .BD࢖ͬͯͯෆศͩͱࢥ͏͜ͱΛղܾ͠Α͏

  40. ࡞Γ͍ͨ.BDΞϓϦ͕ͳ͍ ˣ !ZJNBKP͕3Y4XJGUؚΉ ٕज़ΞυόΠε੥͚ෛ͍·͢Αʂʂ ͝૬ஊԼ͍͞

  41. ͓ΘΓ ܁Γฦ͠ʹͳΓ·͕͢ ˒Λ͚ͭͯ๨Εͳ͍Α͏ʹ͓ͯ͘͠ͱ͍͍ͷ͔΋ Ջͳ࣌ʹ1VMM3FRVFTU͘ΕΔͱخ͍͠Ͱ͢ IUUQTHJUIVCDPNZJNBKP.FFUVQ5XFFU