Home Ask Login Register

Developers Planet

Your answer is one click away!

Bear with me February 2016

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?


marosoaie February 2016

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

Post Status

Asked in February 2016
Viewed 3,237 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life