我正在尝试使用NSKeyedUnarchiver解码URL对象数组。这是代码:

let urlArray: [URL] = [URL(string: "https://apple.com")!,
                       URL(string: "https://google.com")!]

do {
    let archivedUrls = try NSKeyedArchiver.archivedData(withRootObject: urlArray, requiringSecureCoding: false)
    let _ = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: archivedUrls)
} catch {
    print(error)
}

我收到以下错误:
Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'NS.objects' was of unexpected class 'NSURL'. Allowed classes are '{(
    NSArray
)}'." UserInfo={NSDebugDescription=value for key 'NS.objects' was of unexpected class 'NSURL'. Allowed classes are '{(
    NSArray
)}'.}

如果我将let _ = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: archivedUrls)替换为let _ = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, NSURL.self], from: archivedUrls),那么它将起作用。但这意味着它可以解码NSArrayNSURL对象,而不是包含NSArray对象的NSURL

如果我将数组更改为String数组,则一切正常:
let stringArray: [String] = ["string", "string2"]

do {
    let archivedStrings = try NSKeyedArchiver.archivedData(withRootObject: stringArray, requiringSecureCoding: false)
    let _ = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: archivedStrings)
} catch {
    print(error)
}

有人对此行为有解释吗?

最佳答案

如果不需要安全编码(requiringSecureCoding: false),则可以使用unarchiveTopLevelObjectWithData(_:)

do {
    let archivedUrls = try NSKeyedArchiver.archivedData(withRootObject: urlArray, requiringSecureCoding: false)
    if let urls = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archivedUrls) as? [URL] {
        print(urls)
    } else {
        print("not URLs")
    }
} catch {
    print(error)
}

或者,您可以使用unarchivedObject(ofClasses:from:)指定包含在归档中的类型。
do {
    let archivedUrls = try NSKeyedArchiver.archivedData(withRootObject: urlArray, requiringSecureCoding: true)
    if let urls = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSArray.self, NSURL.self], from: archivedUrls) as? [URL] {
        print(urls)
    } else {
        print("not URLs")
    }
} catch {
    print(error)
}
NSString似乎是该规则的例外。

10-08 05:34