David David - 5 months ago 55
Swift Question

EXC_BAD_ACCESS using self.performSelector

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:



//: Playground - noun: a place where people can play

import UIKit

class Foo: NSObject {

func timer() {
self.performSelector( #selector(Foo.print))

@objc func 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? {
        return nil

I believe the code runs in the Playground too.

performSelectors return type is not Void.

- performSelector:

func performSelector(_ aSelector: Selector) -> Unmanaged<AnyObject>!

So, Playground tries to get the result value to display. Which in fact is not returned.