自定义类到CoreData并返回

时间:2018-05-17 12:13:04

标签: swift core-data

我通过Decodable解析jason并创建一个对象数组,但无法找到将其保存在核心数据中的方法,

这是模型:

import UIKit

public struct  Movie : Decodable {
    let title : String
    let image : String
    let rating : Double
    let releaseYear : Int
    let genre : Array<String>


    init(title: String ,image : String , rating : Double, releaseYear : Int, genre : Array<String>) {
        self.title = title
        self.image = image
        self.rating = rating
        self.releaseYear = releaseYear
        self.genre = genre

    }
    init() {
        self.title = "title"
        self.image = "image"
        self.rating = 0.0
        self.releaseYear = 0000
        self.genre = ["",""]
    }
}

这是我到目前为止所做的事情

URLSession.shared.dataTask(with: url) { (data, response, err) in
             guard err == nil else { return }
            guard let data = data else { return }
            do {
                let movies = try JSONDecoder().decode([Movie].self, from: data)
                self.movieList = movies
                DispatchQueue.main.async {
                    self.collectionView.reloadData()
                }

            } catch let jsonErr {
                print("Error serializing json:", jsonErr)
            }
            }.resume()

尝试创建具有各种属性类型的实体,但都是徒劳的

0 个答案:

没有答案