Explore Library
Code Quiz

State Restoration Key Mismatch Bug

Find the subtle bug that prevents a view controller's state from surviving process death.

Codeswift
class SearchViewController: UIViewController {
    var lastQuery: String = ""

    override func encodeRestorableState(with coder: NSCoder) {
        super.encodeRestorableState(with: coder)
        coder.encode(lastQuery, forKey: "lastQuery")
    }

    override func decodeRestorableState(with coder: NSCoder) {
        super.decodeRestorableState(with: coder)
        lastQuery = coder.decodeObject(forKey: "searchQuery") as? String ?? ""
    }
}

After the app is killed by the system and relaunched (warm start), lastQuery is always empty even though restoration is enabled. What is the bug?