我正在更新我的应用程序以支持 IOS 11 版本目前它适用于 IOS 13.2 但在 IOS 11 和 IOS 12.2 我在 NSKeyedUnarchiver decodeObject 有 EXC_BAD_INSTRUCTION
有错误的类
class Seance : NSObject, NSCoding{
var date: NSDate
var action: [String:ObjectifData] = Seance.actionStartupPoint()
var duration = 0.0
static var instance: Seance? = nil
static func actionStartupPoint() -> [String: ObjectifData]{
var a: [String: ObjectifData] = [String:ObjectifData]()
for objectif in Storage.instance.objectif{
a[objectif.key] = objectif.value.type.initialise
}
return a;
}
init(date: NSDate){
self.date = date;
}
required init(coder decoder: NSCoder){
self.date = decoder.decodeObject(forKey: "date") as! NSDate
self.action = try decoder.decodeObject(forKey: "action") as! [String: ObjectifData] // EXC_BAD_INSTRUCTION ERROR
self.duration = decoder.decodeDouble(forKey: "duration")
}
func encode(with coder: NSCoder){
print(action)
print(date)
print(duration)
coder.encode(date, forKey: "date")
coder.encode(action, forKey: "action")
coder.encode(duration, forKey: "duration")
}
func convertActionToObjectifData() -> [String: Objectif]{
var dic: [String: Objectif] = [:]
for convert in action{
if Storage.instance.objectif[convert.key] != nil {
dic[convert.key] = Storage.instance.objectif[convert.key]!
}
}
return dic
}
}
ObjectifData 是具有 3 个父级的协议
像这样
class BinaireData : NSObject, NSCoding, ObjectifData {
var validate: Bool = false
func encode(with coder: NSCoder) {
coder.encode(validate, forKey: "validate")
}
override init(){}
required init(coder decoder: NSCoder) {
super.init()
self.validate = decoder.decodeBool(forKey: "validate")
}
func getObjectifType() -> ObjectifType {
return .binaire
}
func getPrimaryValue() -> Double {
return validate ? 1 : 0
}
func stringFormatedValue() -> String {
return validate ? "Oui" : "Non"
}
func updateFromWatch(message: [String : Any]) -> Bool {
let value: Bool = message["event"] as! Bool
if(self.validate != value){
self.validate = value
return true
}
return false
}
}
对象数据 =
protocol ObjectifData: NSObject {
func getObjectifType() -> ObjectifType
func getPrimaryValue() -> Double
func stringFormatedValue() -> String
func updateFromWatch(message: [String:Any]) -> Bool}
为了加载这个,我在启动时这样做
training = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as! [String: [Seance]]
经过几项研究后,我注意到问题可能是由于 ios11 上的 NSKeyUnarchiver 中不允许的协议而发生的
是真的吗?我该如何解决这个问题?感谢阅读祝你有美好的一天