I get Swift Compiler Error - Command failed due to signal: Segmentation fault: 11" whenever I try override a function of a subclass that inherits from NSObject that is declared within a function.

I've tried it with different classes and functions, I get the error for all of them.

  1. I will only get the error if I am overriding a function or
  2. If I remove the NSObject, it works and I do not get the error.

Anybody know why this is and why inheriting from NSObject makes a difference?

Example:

class ParentClass: NSObject {
    func returnFooString() -> String {
        return "foo"
    }
}

//This Fails
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        class childClass: ParentClass {
            override func returnFooString() -> String {
                return "bar"
            }
        }
    }
}

//This Passes
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        class childClass: ParentClass {
           func returnBarString() -> String {
                return "bar"
            }
        }
    }
}

Overriding the returnFooString function will only pass if ParentClass does not inherit from NSObject

2

There are 2 best solutions below

0
On BEST ANSWER

This seems like it was a bug in the older versions of Swift. I have tested again in Swift 2.0 and it is now fixed

2
On

NSObject extends from Cocoa Class which has it's own init method. When you delete NSObject it turns to a normal swift class and doesn't require Cocoa Class init. That's why it doesn't give error.

Initialization Sample of NSObject

class MyClass: NSObject {

    var someProperty: NSString // no need (!). It will be initialised from controller 

    init(fromString string: NSString) {
        self.someProperty = string
        super.init()
    }

    convenience override init() {
        self.init(fromString:"John") // calls above mentioned controller with default name
    }        
}