Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use sentMessage with subclass? #1288

Closed
onmyway133 opened this issue Jun 10, 2017 · 1 comment
Closed

How to use sentMessage with subclass? #1288

onmyway133 opened this issue Jun 10, 2017 · 1 comment

Comments

@onmyway133
Copy link

onmyway133 commented Jun 10, 2017

Hi, I'm trying to use sentMessage with subclass. I have a class called

class ListController: UIViewController {
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    // THIS IS AN OVERRIDDEN METHOD
  }

  func hello() {
    // THIS IS A METHOD FROM SUBCLASS
  }
}

And I use sentMessage

let list = ListController()

list.rx.sentMessage(#selector(ListController.hello)).subscribe(onNext: {
  print($0)
})

list.rx.sentMessage(#selector(ListController.viewDidAppear(_:))).subscribe(onNext: {
  print($0)
})

list.viewDidAppear(true)
list.hello()

I see that subscribe is not called at all. It seems that sentMessage does not work for subclass if

  • it overrides the method
  • it introduces a new method

What I'm trying to do is onmyway133/blog#55. I've just read this #308 so maybe there's some clues there.

I don't like swizzling, just want to see how it works with this case

@tarunon
Copy link
Contributor

tarunon commented Jun 13, 2017

@onmyway133 Hello, I think this problem is caused difference of method invocation between Swift and Objective-C.
Actually, NSObject.rx.sentMessage and NSObject.rx.methodInvoked is based on method swizzling, Swift method invocation based v-table and it's not affected by method swizzling.
If you want to use objc_msgSend way in swift, you can use dynamic modifier on your function.

class ListController: UIViewController {
  override dynamic func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    // THIS IS AN OVERRIDDEN METHOD
  }

  dynamic func hello() {
    // THIS IS A METHOD FROM SUBCLASS
  }
}

But in my opinion, it's not good that using NSObject.rx.sentMessage and NSObject.rx.methodInvoked a lot.

@kzaher kzaher closed this as completed Jun 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants