title: How to save custom type in CoreData with Codable Protocol Swift
body: <p>I am trying to save the custom object of type codable, In which I am able to store Int16 type. But for [Movie] type in Coredata its NSObject, Entity I have an attribute movie is of type Transformable.</p>
  
  <blockquote>
    <p>Error: No 'decodeIfPresent' candidates produce the expected contextual
    result type 'NSObject?'</p>
  </blockquote>
  
  <p>How can save this custom type Array with Transformable type</p>
  
  <pre><code>class MovieResults: Results, Codable {
  
          required convenience public init(from decoder: Decoder) throws  {
              guard let codingUserInfoKeyManagedObjectContext = CodingUserInfoKey.context,
                  let managedObjectContext = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext,
                  let entity = NSEntityDescription.entity(forEntityName: "Results", in: managedObjectContext) else {
                      fatalError("Failed to retrieve managed object context")
              }
  
              self.init(entity: entity, insertInto: managedObjectContext)
  
              let container = try decoder.container(keyedBy: CodingKeys.self)
  
              self.page = try container.decodeIfPresent(Int16.self, forKey: .page) ?? 0
              self.numberOfResults = try container.decodeIfPresent(Int16.self, forKey: .numberOfResults) ?? 0
              self.numberOfPages = try container.decodeIfPresent(Int16.self, forKey: .numberOfPages) ?? 0
              self.movies = try container.decodeIfPresent([Movie].self, forKey: .movies) ?? nil
          }
  
          // MARK: - Encodable
          public func encode(to encoder: Encoder) throws {
              var container = encoder.container(keyedBy: CodingKeys.self)
              try container.encode(page, forKey: .page)
              try container.encode(numberOfResults, forKey: .numberOfResults)
              try container.encode(numberOfPages, forKey: .numberOfPages)
              try container.encode(movies, forKey: .movies)
          }
  
          private enum CodingKeys: String, CodingKey {
              case page
              case numberOfResults = "total_results"
              case numberOfPages = "total_pages"
              case movies = "results"
          }
      }
  </code></pre>
  
  <blockquote>
    <p>Movie Array is an custom attribute of type Transformable in CoreData</p>
  </blockquote>
  
  <pre><code>class Movies: Movie, Codable {
      public func encode(to encoder: Encoder) throws {
  
      }
  
      required convenience init(from decoder: Decoder) throws {
          guard let codingUserInfoKeyManagedObjectContext = CodingUserInfoKey.context,
              let managedObjectContext = decoder.userInfo[codingUserInfoKeyManagedObjectContext] as? NSManagedObjectContext,
              let entity = NSEntityDescription.entity(forEntityName: "Movie", in: managedObjectContext) else {
                  fatalError("Failed to decode User")
          }
  
          self.init(entity: entity, insertInto: managedObjectContext)
  
          let container = try decoder.container(keyedBy: CodingKeys.self)
  
          self.identifier = try container.decodeIfPresent(Int16.self, forKey: .identifier) ?? 0
          self.posterPath = try container.decodeIfPresent(String.self, forKey: .identifier)
          self.backdrop = try container.decodeIfPresent(String.self, forKey: .identifier)
          self.title = try container.decodeIfPresent(String.self, forKey: .identifier)
          self.releaseDate = try container.decodeIfPresent(String.self, forKey: .identifier)
          self.rating = try container.decodeIfPresent(Int16.self, forKey: .rating) ?? 0
          self.overview = try container.decodeIfPresent(String.self, forKey: .identifier)
      }
  
      enum CodingKeys: String, CodingKey {
          case identifier
          case posterPath = "poster_path"
          case backdrop = "backdrop_path"
          case title
          case releaseDate = "release_date"
          case rating = "vote_average"
          case overview
      }
  }
  </code></pre>
  
  <blockquote>
    <p>With this, it's working fine.</p>
  </blockquote>
  
  <pre><code>self.movies = try container.decodeIfPresent([Movies].self, forKey: .movies)! as NSObject
  </code></pre>
  
  <blockquote>
    <p>I am Inheriting NSManagedObject Class Is this Correct way. I tried
    using the extension but it throws an error for initializers.?</p>
  </blockquote>
  
  <pre><code>public convenience init(from decoder: Decoder) throws
  </code></pre>
  
  <blockquote>
    <p>Initializer requirement 'init(from:)' can only be satisfied by a
    'required' initializer in the definition of non-final class
    'MovieResult'</p>
  </blockquote>