iosswiftstate-restoration

Provide default value for restorationIdentifier


I want to override the restorationIdentifier variable so all view controllers inheriting from MyViewController have the restorationIdentifier set to their class names. However, it seems like Swift will not allow me doing this.

The var I want to overload and provide default implementation is defined as:

var restorationIdentifier: String?

I tried overriding it with:

class MyViewController : UIViewController {

    override var restorationIdentifier: String? {
        return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
    }
}

Compiler shouts at me with:

Getter for restorationIdentifier with Objective-C selector restorationIdentifier conflicts with getter for restorationIdentifier from superclass UIViewController with the same Objective-C selector

How can I overcome this?


Solution

  • You are not defining the setter, which makes it a read-only property. This code works for me:

    override var restorationIdentifier: String? {
        get {
            return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
        }
        set(value) {
            super.restorationIdentifier = value
        }
    }