简体   繁体   English

Swift NSSecureCoding 在解包时意外发现 nil

[英]Swift NSSecureCoding unexpectedly found nil while unwrapping

I am storing a transformable in CoreData and am trying to make it NSSecureCoding compliant, but I keep getting Unexpectedly found nil whilst unwrapping an optional value, when I try to decode我在 CoreData 中存储了一个可转换文件,并试图使其符合 NSSecureCoding,但是当我尝试解码时,在解开可选值的同时意外发现 nil

in the XCDataModel, I am set the property as transformable and use the custom class AssignedExerciseObjects, I have also set the transformer to AssignedExerciseObjectsValueTransformer在 XCDataModel 中,我将属性设置为可转换并使用自定义类 AssignedExerciseObjects,我还将转换器设置为 AssignedExerciseObjectsValueTransformer

I have also registered the transformer in my CoreData stack我还在我的 CoreData 堆栈中注册了变压器

here is a copy of my transformer这是我的变压器的副本

@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)
}
}

and here is a copy of my custom class这是我的自定义类的副本

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))
    
    }
}

this is where it gives me the error这是它给我错误的地方

let mAssignedExerciseObjects = aDecoder.decodeObject(of: NSArray.self, forKey: Key.assignedExerciseObjects.rawValue) as! [AssignedExerciseObject]

I'm still fairly new to swift, but I cannot for the life of me work out why it is returning a nil value?我对 swift 还是很陌生,但我终生无法弄清楚为什么它返回 nil 值?

when the record is created, it all seems to work fine, but when I try to display the record in a table, I get the error创建记录后,似乎一切正常,但是当我尝试在表中显示记录时,出现错误

any help is greatly appreciated任何帮助是极大的赞赏

regards问候

Jamie杰米

OK, I've fixed it好的,我已经修好了

Adding my custom class fixed the issue添加我的自定义类解决了这个问题

 let mAssignedExerciseObjects = aDecoder.decodeObject(of: [AssignedExerciseObject.self, NSArray.self], forKey: Key.assignedExerciseObjects.rawValue) as! [AssignedExerciseObject]
   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM