hI:我一直在使用EVReflection使我们的网络层完全休息,我必须说:真棒!感谢@evermeer为这个图书馆。你可以在这里得到它:https://github.com/evermeer/EVReflection
现在,问题:
下一步是将这些对象直接放入CORE DATA。这是问题中的一个类
//这是带有EVReflectable Extension的对象作为文档声明:
import Foundation
import CoreData
import EVReflection
public class NGTripSummary: NSManagedObject { }
extension NGTripSummary: EVReflectable { }
//和HERE是对象属性:
// NGTripSummary + CoreDataProperties.swift
import Foundation
import CoreData
extension NGTripSummary {
@nonobjc public class func fetchRequest() -> NSFetchRequest<NGTripSummary> {
return NSFetchRequest<NGTripSummary>(entityName: "NGTripSummary")
}
@NSManaged public var carId: Int64
@NSManaged public var citiesVisited: NSObject?
@NSManaged public var cost: Double
@NSManaged public var distance: Double
@NSManaged public var globalStartDate: NSDate?
@NSManaged public var globalEndDate: NSDate?
@NSManaged public var kpl: Double
@NSManaged public var litres: Double
@NSManaged public var routeLocations: NSObject?
@NSManaged public var sessionId: Int64
@NSManaged public var localStartDate: NSDate?
@NSManaged public var localEndDate: NSDate?
@NSManaged public var duration: Int64
@NSManaged public var speed: Double
@NSManaged public var _id: Int64
@NSManaged public var sessionUuid: String?
@NSManaged public var tripUuid: String?
}
//这是表示演示对象的JSON字符串:
let tripData = "{\"id\":26105240,\"userId\":25796277,\"carId\":25817551,\"vehicleId\":57812351,\"sessionUuid\":\"53324259-aa69-41c8-8f9e-c62bdb70f165\",\"tripUuid\":\"afdd8f55-6d14-4cf9-bd9f-5b6da47aaf93\",\"localStartDate\":1487170622490,\"localEndDate\":1487178323654,\"globalStartDate\":1487163422490,\"globalEndDate\":1487171123654,\"routeLocations\":null,\"litres\":24.7699,\"kpl\":0.0772,\"cost\":153.3258,\"distance\":1.9132,\"duration\":491.958,\"speed\":14.0}"
//这里的方法是我试图用来创建这个模拟对象:
func makeMockData() {
let singleTrip = NGTripSummary(json: tripData)
print("Single Trip: \(singleTrip)")
}
//现在:创建对象时,崩溃发生在这里@classEVReflection:
// Call your own object validators that comply to the format: validate<Key>:Error:
do {
var setValue: AnyObject? = value as AnyObject?
/* LINE 923: CRASH HAPPENS HERE -> */ try anyObject.validateValue(&setValue, forKey: key)
anyObject.setValue(setValue, forKey: key)
} catch _ {
(anyObject as? EVReflectable)?.addStatusMessage(.InvalidValue, message: "Not a valid value for object `\(NSStringFromClass(type(of: (anyObject as AnyObject))))`, type `\(type)`, key `\(key)`, value `\(value)`")
print("INFO: Not a valid value for object `\(NSStringFromClass(type(of: (anyObject as AnyObject))))`, type `\(type)`, key `\(key)`, value `\(value)`")
}
//////////////////撞击的原因////////////////////
[error]错误:CoreData:错误:无法在NSManagedObject类上调用指定的初始化程序&#39; NGTripSummary&#39; CoreData:错误:CoreData:错误:无法在NSManagedObject类上调用指定的初始化程序&#39; NGTripSummary&#39;
/////////////////////////////////////////////// ///
- &GT;任何人,请帮助:-0
答案 0 :(得分:2)
错误消息描述了确切的问题。您似乎正在使用以下代码行创建browser_callback()
的实例:
> equal <- function(a, b, sig=4) { return (round(a,sig) == round(b,sig)) }
> equal(1.23456, 1.23457)
[1] TRUE
> equal(1.23456, 1.23557)
[1] FALSE
但NGTripSummary
是let singleTrip = NGTripSummary(json: tripData)
的子类,您永远不会为NGTripSummary
调用指定的初始值设定项。这是必需的。您必须在NSManagedObject
上致电NSManagedObject
,或者使用init(entity:insertInto:)
上的工厂方法NSManagedObject
来获取有效的托管对象。如果您没有,则会收到此特定错误,并且您的应用会崩溃。
如果需要使用insertNewObject(forEntityName:into:)
创建实例,则可以执行此操作,但还需要提供托管对象上下文和实体描述。您可以使用类上的便捷初始化程序来执行此操作,这将在初始化过程中调用指定的初始化程序。