This is a simple academic not real code.
I want to run print method using performSelector function. But If I run this code in playground, throws an exception:
EXC_BAD_ACCESS (code=EXC_I386_GPFLT).
Code:
//: Playground - noun: a place where people can play
import UIKit
@objc(Foo)
class Foo: NSObject {
func timer() {
self.performSelector( #selector(Foo.print))
}
@objc func print() {
NSLog("print")
}
}
let instance = Foo()
instance.timer() // <-- EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
Where is the problem?
Try changing your Foo.print()
to something like this:
@objc func print() -> AnyObject? {
NSLog("print")
return nil
}
I believe the code runs in the Playground too.
performSelector
s return type is not Void
.
func performSelector(_ aSelector: Selector) -> Unmanaged<AnyObject>!
So, Playground tries to get the result value to display. Which in fact is not returned.