Save complex JSON to Core Data in Swift

前端 未结 3 1045
名媛妹妹
名媛妹妹 2020-12-20 10:16

I want to save the JSON Result from web service to Core data, following is the code for JSON parsing.

if let jsonResult = try JSONSerialization.jsonObject(wi         


        
3条回答
  •  时光说笑
    2020-12-20 10:43

    I save JSON by converting it in into Data and save that Data in CoreData

    Following Code works for me......

    func saveSpotsLocation(model: SpotsModel, packageId: String, regionName: String) {
      let newUser = NSEntityDescription.insertNewObject(forEntityName: "SpotsDetail", into: context)
      do {
        let data = NSKeyedArchiver.archivedData(withRootObject: model.dictionaryRepresentation())
        newUser.setValue(data, forKey: "data")
        newUser.setValue(packageId, forKey: "packageId")
        newUser.setValue(regionName, forKey: "regionName")
        try context.save()
      } catch {
        print("failure")
      }
    }
    

提交回复
热议问题