daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest May 16th, 2018 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Article.swift
  3. //  Veille
  4. //
  5. //  Created by Anthony Da Cruz on 26/01/2018.
  6. //  Copyright © 2018 Anthony Da Cruz. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import CoreData
  11.  
  12. class Article: NSManagedObject, Decodable {
  13.    
  14.     @NSManaged var title:String!
  15.     @NSManaged var summary:String?
  16.     @NSManaged var link:URL!
  17.     @NSManaged var image:URL?
  18.     @NSManaged var createdDate: Date
  19.     //var tags:[String]?
  20.     @NSManaged var id:UUID
  21.    
  22.     enum CodingKeys: String, CodingKey {
  23.         case title
  24.         case summary = "description"
  25.         case link
  26.         case image = "imageURL"
  27.         case createdDate = "date"
  28.     }
  29.    
  30.     required convenience init(from decoder: Decoder) throws {
  31.        
  32.         guard let contextUserInfoKey = CodingUserInfoKey.context else { fatalError("cannot find context key") }
  33.        
  34.         guard let managedObjectContext = decoder.userInfo[contextUserInfoKey] as? NSManagedObjectContext else { fatalError("cannot Retrieve context") }
  35.        
  36.         guard let entity = NSEntityDescription.entity(forEntityName: "Article", in: managedObjectContext) else { fatalError() }
  37.        
  38.         self.init(entity: entity, insertInto: nil)
  39.        
  40.         let values = try decoder.container(keyedBy: CodingKeys.self)
  41.         self.createdDate = Date()
  42.         self.title = try values.decode(String.self, forKey: .title)
  43.        
  44.         self.summary = try values.decode(String.self, forKey: .summary)
  45.        
  46.         guard let linkString = try values.decodeIfPresent(String.self, forKey: .link) else { return }
  47.         if let linkUrl = URL(string: linkString) {
  48.             self.link = linkUrl
  49.         }
  50.        
  51.         if let imageURLString = try values.decodeIfPresent(String.self, forKey: .image) {
  52.             if let imageURL = URL(string: imageURLString){
  53.                 self.image = imageURL
  54.             }
  55.         }
  56.     }
  57.    
  58.    
  59. }
  60.  
  61. extension Article: Encodable{
  62.    
  63.     public func encode(to encoder: Encoder) throws {
  64.         var container = encoder.container(keyedBy: CodingKeys.self)
  65.         try container.encode(self.title, forKey: .title)
  66.         try container.encodeIfPresent(self.summary, forKey: .summary)
  67.         try container.encodeIfPresent(self.image, forKey: .image)
  68.         try container.encode(self.link, forKey: .link)
  69.         try container.encode(self.createdDate.toIso8601(), forKey: .createdDate)
  70.     }
  71. }
  72.  
  73. extension CodingUserInfoKey {
  74.     static let context = CodingUserInfoKey(rawValue: "context")
  75. }
  76.  
  77. // Use it :
  78. let context = CoreDataStack.store.persistentContainer.newBackgroundContext() //Getting context
  79. let plistDecoderForArticle = PropertyListDecoder()
  80. plistDecoderForArticle.userInfo[CodingUserInfoKey.context!] = context //Pass it to CodingUserInfoKey which is made for that
  81. let decodedData = try plistDecoderForArticle.decode([Article].self, from: data) //Decoding init got the managedObjectContext
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top