0

I am trying to call a function from another view controller with NSSelectorFromString. I have tried this:

MainScreenViewController().perform(NSSelectorFromString("function"))

But the application crashes. Thanks in advance.

4
  • are you using swift 4? Commented Jan 19, 2018 at 10:59
  • Yes it is indeed swift 4 Commented Jan 19, 2018 at 11:02
  • add @ojc as below answer, this is required from swift 4. Commented Jan 19, 2018 at 11:03
  • The perform selector pattern with string argument and NSSelectorFromString is very objective-c-ish. There are better (and more reliable) ways in Swift Commented Jan 19, 2018 at 11:57

1 Answer 1

3

You should add an @objc inference to the function, otherwise your function will not be expressable in Objective-C, which is what NSSelectorFromString uses.

@objc func function() {
    [...]
}

Learn more about the limited @objc inference in Swift 4 here.

Sign up to request clarification or add additional context in comments.

1 Comment

Are you missing the ()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.