title: Swift And CoreData With Custom Class As Transformable Object
body: <p>I am trying to use a custom class with swift and CoreData as a transformable object. I have been wasting hours trying to figure it out but cannot. I keep getting two errors: Property cannot be marked @NSManaged because its type cannot be represented in Objective-C and Property cannot be declared public because its type uses an internal type.<br>
  Help is greatly appreciated. Thanks.</p>
  
  <p>I have a CoreData object named User. I also have an object named Site.<br>
  I don't want the Site object to be a CoreData object because the majority of the times that I create a Site object I don't want it added to the CoreData context.</p>
  
  <p>Below you will see "primarySite" of type "Site" and "sites" which is an array of type "Site". These two lines give the same two errors I listed above.</p>
  
  <p><strong>Data Model</strong>
  <a href="https://i.stack.imgur.com/zxeBo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zxeBo.png" alt="Data Model"></a></p>
  
  <p><strong>User+CoreDataClass</strong></p>
  
  <pre><code>import Foundation
  import CoreData
  
  public class User: NSManagedObject {
  }
  </code></pre>
  
  <p><strong>User+CoreDataProperties</strong></p>
  
  <pre><code>import Foundation
  import CoreData
  
  extension User {
      @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;User&gt; {
          return NSFetchRequest&lt;User&gt;(entityName: "User");
      }
  
      @NSManaged public var firstName: String?
      @NSManaged public var lastName: String?
      @NSManaged public var role: String?
      @NSManaged public var primarySite: Site
      @NSManaged public var sites: [Site]
  }
  </code></pre>
  
  <p><strong>Site</strong></p>
  
  <pre><code>import UIKit
  
  class Site: NSCoding {
      let identifier: Int32
      let name: String
      let note: String
  
      init(with values: Dictionary&lt;String,Any&gt;) {
          identifier = Int32(values["identifier"] as! String)!
          name = values["name"] as! String
          note = values["note"] as! String
      }
  
      required convenience init(coder decoder: NSCoder) {
          var siteValues = [String:Any]()
          siteValues["identifier"] = decoder.decodeObject(forKey: "identifier") as! String
          siteValues["name"] = decoder.decodeObject(forKey: "name") as! String
          siteValues["note"] = decoder.decodeObject(forKey: "note") as!
          self.init(with: siteValues)
      }
  
       func encode(with coder: NSCoder) {
          coder.encode(String(self.identifier), forKey: "identifier")
          coder.encode(self.name, forKey: "name")
          coder.encode(self.note, forKey: "note")
      }
  }
  </code></pre>