我在 CoreData 中存储了一个可转换对象,并试图使其符合 NSSecureCoding 标准,但是当我尝试解码时,在展开可选值时,我一直意外发现 nil
在 XCDataModel 中,我将属性设置为可转换并使用自定义类 AssignedExerciseObjects,我还将转换器设置为 AssignedExerciseObjectsValueTransformer
我还在我的 CoreData 堆栈中注册了转换器
这是我的变压器的副本
@objc(AssignedExerciseObjectsValueTransformer)
final class AssignedExerciseObjectsValueTransformer: NSSecureUnarchiveFromDataTransformer {
static let name = NSValueTransformerName(rawValue: String(describing: AssignedExerciseObjectsValueTransformer.self))
override static var allowedTopLevelClasses: [AnyClass] {
return [NSArray.self, AssignedExerciseObjects.self]
}
/// Registers the transformer.
public static func register() {
let transformer = AssignedExerciseObjectsValueTransformer()
ValueTransformer.setValueTransformer(transformer, forName: name)
}
}
这是我的自定义课程的副本
import Foundation
public class AssignedExerciseObjects: NSObject, NSSecureCoding {
public static var supportsSecureCoding: Bool = true
public var assignedExerciseObjects: [AssignedExerciseObject] = []
enum Key:String {
case assignedExerciseObjects = "assignedExerciseObjects"
}
init(assignedExerciseObjects: [AssignedExerciseObject]) {
self.assignedExerciseObjects = assignedExerciseObjects
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(assignedExerciseObjects, forKey: Key.assignedExerciseObjects.rawValue)
}
public required convenience init?(coder aDecoder: NSCoder) {
let mAssignedExerciseObjects = aDecoder.decodeObject(of: NSArray.self, forKey: Key.assignedExerciseObjects.rawValue) as! [AssignedExerciseObject]
self.init(assignedExerciseObjects: mAssignedExerciseObjects)
}
}
public class AssignedExerciseObject: NSObject, NSSecureCoding {
public static var supportsSecureCoding: Bool = true
public var mainExercise: String = ""
public var prepareTime: String = ""
public var exerciseTime: String = ""
public var highIntensityTime: String = ""
public var restTime: String = ""
public var highLowSplit: Bool = false
public var isCompleted: Bool = false
public var isSkipped: Bool = false
public var order: Double = 0
enum Key:String {
case mainExercise = "mainExercise"
case prepareTime = "prepareTime"
case exerciseTime = "exerciseTime"
case highIntensityTime = "highIntensityTime"
case restTime = "restTime"
case highLowSplit = "highLowSplit"
case isCompleted = "isCompleted"
case isSkipped = "isSkipped"
case order = "order"
}
init(mainExercise: String, prepareTime: String, exerciseTime: String, highIntensityTime: String, restTime: String, highLowSplit: Bool, isCompleted: Bool, isSkipped: Bool, order: Double) {
self.mainExercise = mainExercise
self.prepareTime = prepareTime
self.exerciseTime = exerciseTime
self.highIntensityTime = highIntensityTime
self.restTime = restTime
self.highLowSplit = highLowSplit
self.isCompleted = isCompleted
self.isSkipped = isSkipped
self.order = order
}
public override init() {
super.init()
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(mainExercise, forKey: Key.mainExercise.rawValue)
aCoder.encode(prepareTime, forKey: Key.prepareTime.rawValue)
aCoder.encode(exerciseTime, forKey: Key.exerciseTime.rawValue)
aCoder.encode(highIntensityTime, forKey: Key.highIntensityTime.rawValue)
aCoder.encode(restTime, forKey: Key.restTime.rawValue)
aCoder.encode(highLowSplit, forKey: Key.highLowSplit.rawValue)
aCoder.encode(isCompleted, forKey: Key.isCompleted.rawValue)
aCoder.encode(isSkipped, forKey: Key.isSkipped.rawValue)
aCoder.encode(order, forKey: Key.order.rawValue)
}
public required convenience init?(coder aDecoder: NSCoder) {
let mMainExercise = aDecoder.decodeObject(of: NSString.self, forKey: Key.mainExercise.rawValue)! as String
let mPrepareTime = aDecoder.decodeObject(of: NSString.self,forKey: Key.prepareTime.rawValue)! as String
let mExerciseTime = aDecoder.decodeObject(of: NSString.self,forKey: Key.exerciseTime.rawValue)! as String
let mHighIntensityTime = aDecoder.decodeObject(of: NSString.self,forKey: Key.highIntensityTime.rawValue)! as String
let mRestTime = aDecoder.decodeObject(of: NSString.self,forKey: Key.restTime.rawValue)! as String
let mHighLowSplit = aDecoder.decodeBool(forKey: Key.highLowSplit.rawValue)
let mIsCompleted = aDecoder.decodeBool(forKey: Key.isCompleted.rawValue)
let mIsSkipped = aDecoder.decodeBool(forKey: Key.isSkipped.rawValue)
let mOrder = aDecoder.decodeDouble(forKey: Key.order.rawValue)
self.init(mainExercise: String(mMainExercise), prepareTime:
String(mPrepareTime), exerciseTime: String(mExerciseTime), highIntensityTime: String(mHighIntensityTime), restTime: String(mRestTime), highLowSplit: Bool(mHighLowSplit), isCompleted: Bool(mIsCompleted), isSkipped: Bool(mIsSkipped), order: Double(mOrder))
}
}
这就是它给我错误的地方
let mAssignedExerciseObjects = aDecoder.decodeObject(of: NSArray.self, forKey: Key.assignedExerciseObjects.rawValue) as! [AssignedExerciseObject]
我对 swift 还很陌生,但我一生都无法弄清楚为什么它返回 nil 值?
创建记录时,一切似乎都可以正常工作,但是当我尝试在表格中显示记录时,出现错误
任何帮助是极大的赞赏
问候
杰米