为什么我的核心数据模型保存错误代码=1570 显示?

Why does my Core Data Model Save Error Code=1570 show?

在我的 CoreDataManager class 中,我试图通过设置值来保存我的 CurrentWeather 属性。

然后在 do-catch 块内它尝试保存上下文,然后跳到错误块并打印出错误。我想知道我在这里做错了什么步骤。

I have two classes subclassing NSManagedObject, first WeatherWrapper then CurrentWeather.

func addCurrentWeather(weather: CurrentWeather) -> CurrentWeather? {
    let entity = NSEntityDescription.entity(forEntityName: "CurrentWeather", in: context)!
    let currentWeather = NSManagedObject(entity: entity, insertInto: context)

    currentWeather.setValue(weather.temperature, forKeyPath: "temperature")
    currentWeather.setValue(weather.summary, forKeyPath: "summary")
    currentWeather.setValue(weather.time, forKeyPath: "time")

    do {
        try context.save()
        return currentWeather as? CurrentWeather
    } catch let error as NSError {
        print("Could not save. \(error), \(error.userInfo)")
        return nil
    }
}

Here is the debug console.

Here is WeatherWrapper Model inside it having a property type CurrentWeather

@objc(WeatherWrapper)
public class WeatherWrapper: NSManagedObject, Codable {

    @NSManaged public var latitude: Double
    @NSManaged public var longitude: Double
    @NSManaged public var timezone: String
    @NSManaged public var currentWeather: CurrentWeather

    enum CodingKeys: String, CodingKey {
        case latitude
        case longitude
        case timezone
        case currentWeather = "currently"
    }

    public required convenience init(from decoder: Decoder) throws {
        guard
            let contextUserInfoKey = CodingUserInfoKey.context,
            let managedObjectContext = decoder.userInfo[contextUserInfoKey] as? NSManagedObjectContext,
            let entity = NSEntityDescription.entity(forEntityName: "WeatherWrapper", in: managedObjectContext) else {
                fatalError("Could not retrieve context")
        }

        self.init(entity: entity, insertInto: managedObjectContext)

        let container = try decoder.container(keyedBy: CodingKeys.self)

        latitude = try container.decode(Double.self, forKey: .latitude)
        longitude = try container.decode(Double.self, forKey: .longitude)
        timezone = try container.decode(String.self, forKey: .timezone)
        currentWeather = try container.decode(CurrentWeather.self, forKey: .currentWeather)
    }
}

Here is my CurrentWeather Model, but why have weatherWrapper property? Doesn't make sense to me.

@objc(CurrentWeather)
public class CurrentWeather: NSManagedObject, Codable {

    @NSManaged public var time: Int32
    @NSManaged public var summary: String
    @NSManaged public var temperature: Double
//    @NSManaged public var weatherWrapper: WeatherWrapper

    enum CodingKeys: String, CodingKey {
        case time
        case summary
        case temperature
//        case weatherWrapper
    }

    required convenience public init(from decoder: Decoder) throws {
        guard
            let contextUserInfoKey = CodingUserInfoKey(rawValue: "context"),
            let managedObjectContext = decoder.userInfo[contextUserInfoKey] as? NSManagedObjectContext,
            let entity = NSEntityDescription.entity(forEntityName: "CurrentWeather", in: managedObjectContext) else {
                fatalError("Could not retrieve context")
        }

        self.init(entity: entity, insertInto: managedObjectContext)

        let values = try decoder.container(keyedBy: CodingKeys.self)

        time = try values.decode(Int32.self, forKey: .time)
        summary = try values.decode(String.self, forKey: .summary)
        temperature = try values.decode(Double.self, forKey: .temperature)
//        weatherWrapper = try values.decode(WeatherWrapper.self, forKey: .weatherWrapper)
    }
}

看来你得设置

currentWeather.setValue(<#somevalue#>, forKeyPath: "weatherWrapper")