Abstract functions in Swift Language

kev picture kev · Jun 8, 2014 · Viewed 50.6k times · Source

I'd like to create an abstract function in swift language. Is it possible?

class BaseClass {
    func abstractFunction() {
        // How do I force this function to be overridden?
    }
}

class SubClass : BaseClass {
    override func abstractFunction() {
        // Override
    }
}

Answer

jaumard picture jaumard · Jun 8, 2014

There no concept of abstract in Swift (like Objective-C) but you can do this :

class BaseClass {
    func abstractFunction() {
        preconditionFailure("This method must be overridden") 
    } 
}

class SubClass : BaseClass {
     override func abstractFunction() {
         // Override
     } 
}