1
votes

I have created a generic function for NSKeyedArchiver and NSKeyedUnarchiver. I am able to archive the array data but while doing unarchive facing an issue. Below is my code:

NSKeyedArchiver code:

func cacheData<T>(data: T) {        
        do {
            let codedData = try NSKeyedArchiver.archivedData(withRootObject: data, requiringSecureCoding: false)
        } catch {
            print("Exception while caching data \(error)")
        }
    }

NSKeyedUnarchiver code:

func getCacheData<T>(encodedData: Data, ofClass: T.Type) -> [T]? {
    do{
        if let decodedData = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, T.self as! AnyClass], from: encodedData){
            return decodedData as? [T]
        }
    } catch {
        print("Exception while decode array cache data \(error)")
    }
    return nil
}

Above code works fine for having only strings, integers variables but it failed if having custom classes variables. How to allow these custom classes in NSKeyedUnarchiver.

I am getting below error:

Exception while decode array cache data Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'customclass1' was of unexpected class 'CustomClass1'. Allowed classes are '{( NSArray, MainClass )}'." UserInfo={NSDebugDescription=value for key 'customclass2' was of unexpected class 'CustomClass2'. Allowed classes are '{( NSArray, MainClass )}'.}

Any idea how to solve this?

1
is the custom class extend with Codable protocol?Faysal Ahmed

1 Answers

1
votes

Make sure all your class are confirming to NSCoding. Something like this:

func archiveAndUnarchive() {
let class2 = Class2(value: "Value")
let class1 = Class1(name: "Name", class2: class2)
do {
    // ARCHIVING
    let data = try NSKeyedArchiver.archivedData(withRootObject: class1, requiringSecureCoding: false)

    // UNARCHIVING
    if let decodedData = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? Class1 {
        print(decodedData)
    }
} catch {
    print(error)
}
}

class Class1: NSObject, NSCoding {
var name: String?
var class2: Class2?

func encode(with coder: NSCoder) {
    coder.encode(name, forKey: "name")
    coder.encode(class2, forKey: "class2")
}

required init?(coder: NSCoder) {
    super.init()
    self.name = coder.decodeObject(forKey: "name") as? String ?? ""
    self.class2 = coder.decodeObject(forKey: "class2") as? Class2
}

init(name: String, class2: Class2) {
    super.init()
    self.name = name
    self.class2 = class2
}
}


class Class2: NSObject, NSCoding {
var value: String?

func encode(with coder: NSCoder) {
    coder.encode(value, forKey: "value")
}

required init?(coder: NSCoder) {
    super.init()
    self.value = coder.decodeObject(forKey: "value") as? String
}

init(value: String) {
    super.init()
    self.value = value
}
}