Abstract methods in Swift? Abstract methods in Swift? swift swift

Abstract methods in Swift?


As of today (April 7, 2016), the proposal to introduce abstract classes and methods to Swift (SE-0026) has been deferred.

Joe Groff posted the following in swift-evolution-announce on March 7, 2016:

The proposal has been deferred from Swift 3. Discussion centered around whether abstract classes fit in the direction of Swift as a "protocol-oriented" language. Beyond any religious dogmas, Swift intends to be a pragmatic language that lets users get work done. The fact of the matter today is that one of Swift's primary target platforms is the inheritance-heavy Cocoa framework, and that Swift 2's protocols fall short of abstract classes in several respects [...].

We'd like to revisit this feature once the core goals of Swift 3 have been addressed, so we can more accurately consider its value in the context of a more complete generics implementation, and so we can address the finer points of its design.

I encourage you to read the full email, but I think the conclusion is the same as what you came up with in your question: we're currently stuck with the Objective-C way of doing things (raising exceptions).


There is no Abstract concept in Swift. But we can achieve that scenario by using Inheritance concept like the code below:

class ParentVC:UIViewController {    func loadInformation() {    }}class ChildVC:ParentVC {    // This is an Abstract Method    override func loadInformation() {     }}


How do you define abstract methods, while implementing others?

The "swifty" way of achieving this is combining protocols and extensions, sometimes also typealiases. For data, you are going to define abstract properties in your protocol, then re-define them in a concrete class, then unite all that using a typealias and the & operator:

protocol BaseAbstract: class {    var data: String { get set }    func abstractMethod()    func concreteMethod()}extension BaseAbstract {    // Define your concrete methods that use the abstract part of the protocol, e.g.:    func concreteMethod() {        if !data.isEmpty {            abstractMethod()        }    }}class BaseImpl {    // This is required since we can't define properties in extensions.    // Therefore, we define a class with a concrete property and then     // unite it with the protocol above in the typealias below.    var data: String = "Hello, concrete!"}typealias Base = BaseAbstract & BaseImpl // et voila, `Base` is now ready to be subclassedclass Subclass: Base {    func abstractMethod() { // enforced by the compiler    }}

(It can get tricker if you have generics in this scenario. Currently trying to figure it out.)