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

君の参照は。〜classとfunctionと循環参照の話〜

takasek
September 19, 2016

 君の参照は。〜classとfunctionと循環参照の話〜

2016/9/16 歌舞伎座.tech #11 (http://kbkz.connpass.com/event/37480/) での発表「君の参照は。」の資料です。
発表中に使用した循環参照検証用プロジェクトもあわせてお試し下さい。
https://github.com/takasek/RecursiveReferenceSample

takasek

September 19, 2016
Tweet

More Decks by takasek

Other Decks in Programming

Transcript

  1. 3

  2. 4

  3. ϘλϯΛͻͱ͚ͭͩ࣋ͭ UIViewαϒΫϥεͷίϯϙʔωϯτΛ࡞ͬͨͱ͠·͢ɻ final class MyComponent: UIView { @IBOutlet private var

    button: UIButton! //Button͕ԡ͞ΕͨΒɺcallbackΛ࣮ߦ var callback: (() -> Void)? @IBAction func didButtonPush() { callback?() } } 8
  4. ͜ͷίϯϙʔωϯτʹcallbackͱͯؔ͠਺Λ౉ͯ͠ɺ ViewControllerͰॲཧͰ͖ΔΑ͏ʹ͠·͢ɻ final class ViewController: UIViewController { @IBOutlet private var

    component: MyComponent! override func viewDidLoad() { super.viewDidLoad() component.callback = { ͳΜ͔ॲཧΛ͢Δ() } } deinit { print("deinit") } } 9
  5. final class ViewController: UIViewController { @IBOutlet private var component: MyComponent!

    override func viewDidLoad() { super.viewDidLoad() component.callback = { ͳΜ͔௕͍ॲཧ() if something.isͳΜͨΒ { ଞͷ௕͍ॲཧ() } else { something.΄͛΄͛() } } } deinit { print("deinit") } } ! callbackͷத਎͕๲ΒΜͰ͖ͨ 10
  6. final class ViewController: UIViewController { @IBOutlet private var component: MyComponent!

    override func viewDidLoad() { super.viewDidLoad() component.callback = { self.doHogehoge() } } private func doHogehoge() { ... } deinit { print("deinit") } } ☺ ϝιουʹ੾Γग़ͯ͠ɺ͖ͬ͢Γ 11
  7. !॥؀ࢀর self ➡ self.component self.component ➡ self.component.callback self.component.callback ➡ self

    ɹ let closure = { //Ϋϩʔδϟ͕selfΛڧࢀরͰΩϟϓνϟ͍ͯ͠Δ self.ʙʙʙ } 13
  8. final class ViewController: UIViewController { @IBOutlet private var component: MyComponent!

    override func viewDidLoad() { super.viewDidLoad() component.callback = { [weak self] in self?.doHogehoge() } } private func doHogehoge() { // ... } deinit { print("deinit") } } ɹ ☺ ͖ͬ͢ΓΞήΠϯ 15
  9. ... { ... component.callback = { [weak self] in self?.doHogehoge()

    } } private func doHogehoge() { // ... } ! ͍΍ɺͰ΋଴ͯΑ… ɹ ͜ͷΫϩʔδϟɺແବ͡Όͳ͍͔ʁ 16
  10. ! ͦ͏ͩɺSwiftͰ͸ ɹ ؔ਺΋ୈҰڃΦϒδΣΫτͩ final class MyComponent: UIView { var

    callback: (() -> Void)? } final class ViewController: UIViewController { private func doHogehoge() { } ! ͦͯ͠ϝιου doHogehoge ΋ ɹ ϓϩύςΟ callback ͱಉ͡ ()->Void ܕ ɹ ͱ͍͏͜ͱ͸… 17
  11. final class ViewController: UIViewController { @IBOutlet private var component: MyComponent!

    override func viewDidLoad() { super.viewDidLoad() component.callback = doHogehoge // ! } private func doHogehoge() { // ... } deinit { print("deinit") } } ! ܕ͸߹ͬͯΔʂ ɹ ͜ͷॻ͖ํ…coool͡Όͳ͍!? ɹ doHogehogeͷதͰ͸self࢖ͬͯͳ͍͠ʂ 19
  12. ϝιου final class ViewController: UIViewController { private func doHogeMethod() {

    print("method") } func configureButtonAction() { component.callback = doHogeMethod } } ݁Ռ͸… 29
  13. ϝιου final class ViewController: UIViewController { private func doHogeMethod() {

    print("method") } func configureButtonAction() { component.callback = doHogeMethod } } ݁Ռ: ॥؀ࢀর! 30
  14. ϝιου([unowned self]ͳΫϩʔδϟ͔ΒݺͿ) final class ViewController: UIViewController { private func doHogeMethod()

    { print("method") } func configureButtonAction() { component.callback = { [unowned self] in self.doHogeMethod() } } } ݁Ռ͸… 31
  15. ϝιου([unowned self]ͳΫϩʔδϟ͔ΒݺͿ) final class ViewController: UIViewController { private func doHogeMethod()

    { print("method") } func configureButtonAction() { component.callback = { [unowned self] in self.doHogeMethod() } } } ݁Ռ: ݈શ! 32
  16. functionܕϓϩύςΟ final class ViewController: UIViewController { let functionTypeProperty = {

    print("function-type property") } func configureButtonAction() { component.callback = functionTypeProperty } } ݁Ռ͸… 33
  17. functionܕϓϩύςΟ final class ViewController: UIViewController { let functionTypeProperty = {

    print("function-type property") } func configureButtonAction() { component.callback = functionTypeProperty } } ݁Ռ: ݈શ! 34
  18. functionܕϓϩύςΟ(தͰselfΛ࢖͏) final class ViewController: UIViewController { let functionTypePropertyUsingSelf = {

    print("function-type property using self", self) } func configureButtonAction() { component.callback = functionTypePropertyUsingSelf } } ݁Ռ͸… 35
  19. functionܕϓϩύςΟ(தͰselfΛ࢖͏) final class ViewController: UIViewController { let functionTypePropertyUsingSelf = {

    print("function-type property using self", self) } func configureButtonAction() { component.callback = functionTypePropertyUsingSelf } } ݁Ռ: ݈શ! 36
  20. let functionTypePropertyUsingSelf = { print("function-type property using self", self) }

    func configureButtonAction() { component.callback = functionTypePropertyUsingSelf } ɹ !଴ͬͯ଴ͬͯɺ͜ΕϚδͰ॥؀ࢀরى͖ͳ͍ͷ!? ͬͯͳΓ·͢ΑͶɻ ϚδͰ͢ɻ 37
  21. ϩʔΧϧfunction final class ViewController: UIViewController { func configureButtonAction() { func

    localFunc() { print("local func") } component.callback = localFunc } } ݁Ռ͸… 39
  22. ϩʔΧϧfunction final class ViewController: UIViewController { func configureButtonAction() { func

    localFunc() { print("local func") } component.callback = localFunc } } ݁Ռ: ݈શ! 40
  23. ϩʔΧϧfunction(தͰselfΛ࢖͏) final class ViewController: UIViewController { func configureButtonAction() { func

    localFuncUsingSelf() { print("local func using self", self) } component.callback = localFuncUsingSelf } } ݁Ռ͸… 41
  24. ϩʔΧϧfunction(தͰselfΛ࢖͏) final class ViewController: UIViewController { func configureButtonAction() { func

    localFuncUsingSelf() { print("local func using self", self) } component.callback = localFuncUsingSelf } } ݁Ռ: ॥؀ࢀর! 42
  25. ϩʔΧϧΫϩʔδϟ final class ViewController: UIViewController { func configureButtonAction() { let

    localClosure = { print("local closure") } component.callback = localClosure } } ݁Ռ͸… 43
  26. ϩʔΧϧΫϩʔδϟ final class ViewController: UIViewController { func configureButtonAction() { let

    localClosure = { print("local closure") } component.callback = localClosure } } ݁Ռ: ݈શ! 44
  27. ϩʔΧϧΫϩʔδϟ(தͰselfΛ࢖͏) final class ViewController: UIViewController { func configureButtonAction() { let

    localClosureUsingSelf = { print("local closure using self", self) } component.callback = localClosureUsingSelf } } ݁Ռ͸… 45
  28. ϩʔΧϧΫϩʔδϟ(தͰselfΛ࢖͏) final class ViewController: UIViewController { func configureButtonAction() { let

    localClosureUsingSelf = { print("local closure using self", self) } component.callback = localClosureUsingSelf } } ݁Ռ: ॥؀ࢀর! 46
  29. ϩʔΧϧΫϩʔδϟ(தͰunowned selfΛ࢖͏) final class ViewController: UIViewController { func configureButtonAction() {

    let localClosureUsingUnownedSelf = { [unowned self] in print("local closure using unowned self", self) } component.callback = localClosureUsingUnownedSelf } } ݁Ռ͸… 47
  30. ϩʔΧϧΫϩʔδϟ(தͰunowned selfΛ࢖͏) final class ViewController: UIViewController { func configureButtonAction() {

    let localClosureUsingUnownedSelf = { [unowned self] in print("local closure using unowned self", self) } component.callback = localClosureUsingUnownedSelf } } ݁Ռ: ݈શ! 48
  31. ͜͜·Ͱͷ·ͱΊ » ݈શ!ͳύλʔϯ » ϝιου([unowned self]ͳΫϩʔδϟ͔ΒݺͿ) » functionܕϓϩύςΟ » functionܕϓϩύςΟ(தͰselfΛ࢖͏)

    » ϩʔΧϧfunction » ϩʔΧϧΫϩʔδϟ » ϩʔΧϧΫϩʔδϟ(தͰunowned selfΛ࢖͏) » ॥؀ࢀর"͕ى͜Δύλʔϯ » ϝιουΛ௚઀౉͢ » ϩʔΧϧfunction(தͰselfΛ࢖͏) » ϩʔΧϧΫϩʔδϟ(தͰselfΛ࢖͏) 49
  32. ͞Βʹ·ͱΊ » Ϋϩʔδϟ͸ɺதͰ࢖ΘΕΔreference typeΛڧࢀরΩϟϓν ϟ͢Δ » unowned, weakΛ࢖͑͹ڧࢀরΩϟϓνϟ͸ආ͚ΒΕΔ » classΠϯελϯε͕࣋ͭϝιου͸ɺself΁ͷࢀরΛ࣋ͭ

    » classΠϯελϯε͕࣋ͭfunctionܕϓϩύςΟ͸ɺselfΛڧࢀ র͠ͳ͍ » தͰselfΛ࢖͏functionܕϓϩύςΟͰ͢ΒɺselfΛڧࢀর ͠ͳ͍ » Կނ…!? 50
  33. 53

  34. functionܕϓϩύςΟ(தͰselfΛ࢖͏) …Λ΋͏Ұ౓Α͘Α͘ݟͯΈΔͱɺ final class ViewController: UIViewController { let functionTypePropertyUsingSelf =

    { print("function-type property using self", self) } } ! ͜ͷfunction͕࡞ΒΕΔ࣌ʹɺ ɹ self( ViewController )ͬͯ·ͩଘࡏͯ͠ͳ͘ͳ͍ʁ ɹ ଘࡏ͠ͳ͍΋ͷ͸ɺࢀর΋Ͱ͖ͳ͍ΑͶʁ 55
  35. ͪͳΈʹɺ ͜ͷselfΛऑࢀরΩϟϓνϟ͠Α͏ͱ͢Δͱ… final class ViewController: UIViewController { let functionTypePropertyUsingSelf =

    { [weak self] in print("function-type property using self", self) } } ऑࢀরʹͰ͖Δͷ͸ɺ classܕɺ͋Δ͍͸classܕઐ༻ͷϓϩτίϧ͚ͩɻ self͸ ViewController ܕͷΠϯελϯεͰ͸ͳ͍ͷͰɺ ऑࢀরΩϟϓνϟ΋ෆՄೳͬͯ͜ͱͰ͢Ͷɻ 56
  36. The Swift Programming Language (Swift 3): Closures ΑΓ Closure expressions

    are unnamed closures written in a lightweight syntax that can capture values from their surrounding context. surrounding contextͬͯͷ͸ɺͦ͏͍͏͜ͱ ʢͩͱࢥ͏ʣɻ 57
  37. ·ͱΊʢ׬શ൛ʣ » Ϋϩʔδϟ͸ɺதͰ࢖ΘΕΔreference typeΛڧࢀ রΩϟϓνϟ͢Δ » unowned, weakΛ࢖͑͹ڧࢀরΩϟϓνϟ͸ආ ͚ΒΕΔ »

    classΠϯελϯε͕࣋ͭϝιου͸ɺself(classΠ ϯελϯε)΁ͷڧࢀরΛ࣋ͭɻ͠Όʔͳ͍ɻ » classΠϯελϯεͷfunctionܕϓϩύςΟͰ͸ɺͦ ΋ͦ΋self ≠ ॴ༗͍ͯ͠ΔclassΠϯελϯεࣗ਎ 59