Attempt to insert non-property list object when trying to save a custom object in Swift 3
<p>I have a simple object which conforms to the <code>NSCoding</code> protocol.</p>

<pre><code>import Foundation

class JobCategory: NSObject, NSCoding {
    var id: Int
    var name: String
    var URLString: String

    init(id: Int, name: String, URLString: String) {
        self.id = id
        self.name = name
        self.URLString = URLString
    }

    // MARK: - NSCoding
    required init(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObject(forKey: "id") as? Int ?? aDecoder.decodeInteger(forKey: "id")
        name = aDecoder.decodeObject(forKey: "name") as! String
        URLString = aDecoder.decodeObject(forKey: "URLString") as! String
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(id, forKey: "id")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(URLString, forKey: "URLString")
    }
}
</code></pre>

<p>I'm trying to save an instance of it in <code>UserDefaults</code> but it keeps failing with the following error.</p>

<blockquote>
  <p>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object  for key jobCategory'</p>
</blockquote>

<p>This is the code where I'm saving in <code>UserDefaults</code>.</p>

<pre><code>enum UserDefaultsKeys: String {
    case jobCategory
}

class ViewController: UIViewController {

    @IBAction func didTapSaveButton(_ sender: UIButton) {
        let category = JobCategory(id: 1, name: "Test Category", URLString: "http://www.example-job.com")

        let userDefaults = UserDefaults.standard
        userDefaults.set(category, forKey: UserDefaultsKeys.jobCategory.rawValue)
        userDefaults.synchronize()
    }
}
</code></pre>

<p>I replaced the enum value to key with a normal string but the same error still occurs. Any idea what's causing this?</p>