title: Class conforming to Codable protocol fails with encodeWithCoder: unrecognized selector sent to instance
body: <p>I know that there are several questions similar to this, that tend to all revolve around the class not conforming to the protocol properly, but that should not be the immediate issue here.</p>
  
  <p>The following is a condensed version of the code that is currently giving me this problem:</p>
  
  <pre><code>enum Binary: Int {
      case a = 0
      case b = 1
      case c = 9
  }
  
  final class MyClass: NSCoder {
      var string: String?
      var date: Date?
      var binary: Binary = .c
  
      override init() { }
  
      enum CodingKeys: CodingKey {
          case string, date, binary
      }
  }
  
  extension MyClass: Codable {
      convenience init(from decoder: Decoder) throws {
          self.init()
  
          let values = try decoder.container(keyedBy: CodingKeys.self)
          string = try values.decode(String.self, forKey: .string)
          date = try values.decode(Date.self, forKey: .date)
          binary = try Binary(rawValue: values.decode(Int.self, forKey: .binary)) ?? .c
      }
  
      func encode(to encoder: Encoder) throws {
          var container = encoder.container(keyedBy: CodingKeys.self)
          try container.encode(string, forKey: .string)
          try container.encode(date, forKey: .date)
          try container.encode(binary.rawValue, forKey: .binary)
      }
  }
  </code></pre>
  
  <p>I have created the following class which then attempts to call <code>MyClass</code> with the purpose of writing &amp; reading it to <code>UserDefaults</code>:</p>
  
  <pre><code>class MyClassController {
      private let myClass: MyClass
  
      init() {
          self.myClass = MyClass()
          self.myClass.string = "string"
          self.myClass.date = Date()
          self.myClass.binary = .a
      }
  
      func writeMyClass() {
          let encodedData = NSKeyedArchiver.archivedData(withRootObject: myClass)
          UserDefaults.standard.set(encodedData, forKey: String(describing: MyClass.self))
      }
  
      func readMyClass() {
          if let decoded = UserDefaults.standard.object(forKey: String(describing: MyClass.self)) as? Data,
              let myClass = NSKeyedUnarchiver.unarchiveObject(with: decoded as Data) as? MyClass {
              print("string: \(myClass.string ?? "nil") date: \(myClass.date ?? Date()) binary: \(myClass.binary)")
          }
      }
  }
  </code></pre>
  
  <p>As soon as I call the writeMyClass function though, I get this error:</p>
  
  <blockquote>
    <p>[DemoDecoder.MyClass encodeWithCoder:]: unrecognized selector sent to
    instance #blahblah#</p>
  </blockquote>
  
  <p>Two things I have also tried:</p>
  
  <ul>
  <li>Adding <code>func encode(with aCoder: NSCoder)</code> to <code>MyClass</code></li>
  <li>Removed all properties from <code>MyClass</code> &amp; <code>CodingKeys</code> and the init/encode functions</li>
  </ul>