Abstract functions in Swift Language Abstract functions in Swift Language swift swift

Abstract functions in Swift Language


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     } }


What you want is not a base class, but a protocol.

protocol MyProtocol {    func abstractFunction()}class MyClass : MyProtocol {    func abstractFunction() {    }}

If you don't supply abstractFunction in your class it's an error.

If you still need the baseclass for other behaviour, you can do this:

class MyClass : BaseClass, MyProtocol {    func abstractFunction() {    }}


I port a fair amount of code from a platform that supports abstract base classes to Swift and run in to this a lot. If what you truly want is the functionality of an abstract base class, then that means that this class serves both as an implementation of shared based class functionality (otherwise it would just be an interface/protocol) AND it defines methods that must be implemented by derived classes.

To do that in Swift, you will need a protocol and a base class.

protocol Thing{    func sharedFunction()    func abstractFunction()}class BaseThing{    func sharedFunction()    {        println("All classes share this implementation")    }}

Note that the base class implements the shared method(s), but does not implement the protocol (since it doesn't implement all of the methods).

Then in the derived class:

class DerivedThing : BaseThing, Thing {    func abstractFunction()     {        println("Derived classes implement this");    }}

The derived class inherits sharedFunction from the base class, helping it satisfy that part of the protocol, and the protocol still requires the derived class to implement abstractFunction.

The only real downside to this method is that since the base class does not implement the protocol, if you have a base class method that needs access to a protocol property/method you will have to override that in the derived class, and from there call the base class (via super) passing self so that the base class has an instance of the protocol with which to do its work.

For example, lets say that sharedFunction needed to call abstractFunction. The protocol would stay the same, and the classes would now look like:

class BaseThing{    func sharedFunction(thing: Thing)    {        println("All classes share this implementation")        thing.abstractFunction()    }}class DerivedThing : BaseThing, Thing {    func sharedFunction()    {        super.sharedFunction(self)    }    func abstractFunction()     {        println("Derived classes implement this");    }}

Now the sharedFunction from the derived class is satisfying that part of the protocol, but the derived class is still able to share the base class logic in a reasonably straightforward way.