arraysswiftnskeyedunarchiver

NSKeyedUnarchiver changes in iOS12 - How to unarchive an Array of Strings


There were some changes in Foundation from iOS 11.4 to iOS12. Unfortunately I couldn't find any helpful documentation on this topics.

Before iOS12 i had this code working perfectly to read an Array with Strings from a certain filePath:

if let myList : Array<String> = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? Array<String> {
   // ...
}

As I found out, there are some new methods in iOS12 that I should use and I tried this (in a do-catch-structure of course, and after getting the data object):

let myList : Array<String> = try NSKeyedUnarchiver.unarchivedObject(ofClass: Array<String>, from: data)

I also tried this without success:

let myList : Array<String> = try NSKeyedUnarchiver.unarchivedObject(ofClass: Array<String>.self, from: data)

Any recommendation?


Solution

  • Finally I found out myself. It worked with this method:

    if let fileNames = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(dataObject) as? Array<String> {
        // ... 
    }