swiftios11ios12nskeyedunarchiverios13.2

NSKeyedUnarchiver EXC_BAD_INSTRUCTION with protocol


I am updating my application to support IOS 11 versions Currently it works on IOS 13.2 But on IOS 11 and IOS 12.2 I have EXC_BAD_INSTRUCTION at NSKeyedUnarchiver decodeObject

The class with the error

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 is Protocol with 3 parent

Like this

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

ObjectifData =

protocol ObjectifData: NSObject {

func getObjectifType() -> ObjectifType
func getPrimaryValue() -> Double
func stringFormatedValue() -> String
func updateFromWatch(message: [String:Any]) -> Bool}

And for load this I do this at startup

training = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as! [String: [Seance]]

Breakpoint Stack trace

after several research I noticed that it is possible that the problem occurs because of the protocols that are not allowed in the NSKeyUnarchiver on ios11

is it true ? How can I fix the problem? Thanks for reading Have a good day


Solution

  • You are attempting to encode and decode an object typed as [String: ObjectifData], where ObjectifData is a protocol. You can't do that; a protocol is not a real type! I'm not surprised about the crash; I'm only amazed that this doesn't crash in iOS 13 as well.