Modeling an abstract base class in Swift

Viewed 180

I have a Cocoa Touch class BaseViewController:

class BaseViewController: Named, UIViewController {
    open var name = "Emma"

    override func viewDidAppear() {
        self.doSomethingImportant(self.name)
    }
}

protocol Named {
    var name: String
}

The intent behind BaseViewController is that all view controllers in my app will be derived from it, because it does something important in viewDidAppear() with name.

However, I'm trying to model this in a way where it'll be a compile-time error to subclass from BaseViewController without specifying name, and not have to supply a default name value in the base class. In other words:

  • Can I have BaseViewController require that its subclasses implement Named without it implementing Named itself?
  • If not, how else can I achieve this within the paradigm of Cocoa Touch plus Protocol-Oriented Programming?
0 Answers
Related