JuJoDi JuJoDi - 1 year ago 83
Swift Question

Swift - class method which must be overridden by subclass

Is there a standard way to make a "pure virtual function" in Swift, ie. one that must be overridden by every subclass, and which, if it is not, causes a compile time error?

Answer Source

There isn't any support for abstract class/ virtual functions, but you could probably use a protocol for most cases:

protocol SomeProtocol {
    func someMethod()

class SomeClass: SomeProtocol {
    func someMethod() {}

If SomeClass doesn't implement someMethod, you'll get this compile time error:

error: type 'SomeClass' does not conform to protocol 'SomeProtocol'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download