如何在使用coredata NSManagedObject映射时在swift4编码中编码/解码NSOrderedSet变量?
import CoreData
import Foundation
@objc(TestObject)
public class TestObject:NSManagedObject,Encodable
{
@nonobjc public class func fetchRequest() -> NSFetchRequest<TestObject> {
return NSFetchRequest<TestObject>(entityName: "TestObject")
}
@NSManaged public var testName: String?
@NSManaged public var devices: NSOrderedSet? // of entity type "Device"
enum CodingKeys: String, CodingKey {
case testName
case devices
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy:CodingKeys.self)
try container.encode(testName,forKey:.testName)
try container.encode(devices,forKey:.devices)
}
}
public required convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
try container.decode(testName,forKey:.testName)
try container.decode(devices,forKey:.devices)
}
它给出了错误:编译器给出了这个错误
No 'decode' candidates produce the expected contextual result type 'NSOrderedSet?'