Does not conform to protocol 'NSCoding' - Swift 3
Asked Answered
S

1

9

I have seen several questions similar to mine; however, those are pertaining to swift 2/1 and I am currently using swift 3. I believe Apple has changed it slightly.

class Person: NSObject, NSCoding {

    var signature: UIImage

    init(signature: UIImage) {
        self.signature = signature
    }

    required convenience init(coder aDecoder: NSCoder) {
        let signature = aDecoder.decodeObject(forKey: "signature") as! UIImage
        self.init(signature: signature)
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encode(signature, forKey: "signature")
    }

}

You will notice how Swift 3 now forces me to use required convenience init( instead of required init(. Perhaps that has something to do with it.

How can I resolve this issue? Thanks!

Sallie answered 20/6, 2016 at 14:31 Comment(1)
Did you try "Edit -> Convert -> To Current Swift Syntax" in Xcode? That should fix the problem automatically.Aspire
R
33

The encode method in Swift 3 has been renamed to

func encode(with aCoder: NSCoder) 

When you get the do not conform error you can easily find out which required methods are missing

  • Press ⌘B to build the code.
  • Press ⌘4 to show the issue navigator.
  • Click on the disclosure triangle in front of the issue line.
Redeemer answered 20/6, 2016 at 14:38 Comment(2)
Works perfectly. I will accept in 6 minutes once SO let's me do so :)Sallie
I can believe i wasted my 2 precious days just for this.. Hate you swift for such a change.. and love you bro for saving my ass. :) Thanks a lottttCriminate

© 2022 - 2024 — McMap. All rights reserved.