使用NSKeyedUnarchiver

时间:2019-11-24 17:48:21

标签: ios swift nskeyedunarchiver

我正在尝试使用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)
}

有人对此行为有解释吗?

1 个答案:

答案 0 :(得分:1)

如果您不需要安全编码(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似乎是该规则的例外。