【问题标题】:Unable to find key for specefic value in swift Dictionary无法在快速字典中找到特定值的键
【发布时间】:2021-12-26 05:12:17
【问题描述】:

我试图从 URLSession 错误中获取 tempFileName 并将 resumeData 解码为具有以下代码的字典

let resumeDictionary = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [AnyHashable : Any]

resumeDictionary 包含以下字符串

[AnyHashable("$archiver"): NSKeyedArchiver, AnyHashable("$objects"): <__nscfarray>( $null, { "$class" = "{value = 19}"; "NS.keys" = ( "{值 = 2}", "{值 = 3}", "{值 = 4}", "{值 = 5}", "{值 = 6}", "{值 = 7}", "{值 = 8}", “{值 = 9}” ); “NS.objects”=( "{值 = 10}", "{值 = 12}", "{值 = 13}", "{值 = 14}", "{值 = 15}", "{值 = 16}", "{值 = 17}", “{值 = 18}” ); }, NSURLSessionResumeCurrentRequest, NSURLSessionResumeOriginalRequest, NSURLSessionDownloadURL, NSURLSessionResumeInfoTempFileName, NSURLSessionResumeBytesReceived, NSURLSessionResumeEntityTag,NSURLSessionResumeInfoVersion, NSURLSessionResumeServerDownloadDate, { "$class" = "{value = 11}"; “NS.data” = {长度 = 2427,字节 = 0x62706c69 73743030 d4010203 04050607 ... 00000000 0000083b }; }, { "$classes" = ( NSMutableData, NSData, NSObject ); "$classname" = NSMutableData; }, { "$class" = "{value = 11}"; “NS.data” = {长度 = 1666,字节 = 0x62706c69 73743030 d4010203 04050607 ... 00000000 00000594 }; }, https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1280_10MG.mp4, CFNetworkDownload_BVLTmU.tmp,871422,“5f074cdf-962771”,5,星期四,09 格林威治标准时间 2020 年 7 月 16:59:11,{ "$classes" = ( NSMutableDictionary, NS词典, NSObject ); "$classname" = NSMutableDictionary; } ) , AnyHashable("$top"): { NSKeyedArchiveRootObjectKey = "{值 = 1}"; }, AnyHashable("$version"): 100000]

我想要在这个字符串中的值是“CFNetworkDownload_BVLTmU.tmp”,我不知道如何在代码中得到它我没有看到任何关键值有人可以帮助我 ?

【问题讨论】:

    标签: swift string xcode nsdictionary urlsession


    【解决方案1】:

    如果我做对了,您正试图通过其值将特定元素查找到 Dictionary&lt;AnyHasable, Any&gt;... 然后您必须依赖 first(where:) 并在闭包中将元素的值类型转换为您要查找的值(必须符合 Equatable),然后检查是否相等。

    类似的东西:

    if let element = dictionary.first(where: { ($0 as? Int) == valueToFind }) {
       // do what you have to do with such element…
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-16
      • 2019-11-07
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      相关资源
      最近更新 更多