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.
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.
()?
perform selectorpattern with string argument andNSSelectorFromStringis very objective-c-ish. There are better (and more reliable) ways in Swift