Swift 4.2 如果 let 在 Xcode 11.2 上使用 CMTime 失败

Swift 4.2 if let fails with CMTime on Xcode 11.2

自从升级到 Xcode 11.2 并构建和 运行 我的应用程序后,Swift if let 块报告失败,而这在其他情况下一直有效。这是失败的代码。

public func remoteClient(receivedData data: [String : Any]?, fromPeer peerID: MCPeerID) {

  let params = data?["params"] as? [String:Any] 

  NSLog("Received image \(params!["timestamp"])")

  if let imgData = params?["image"] as? Data, let timeStamp = params?["timestamp"] as? CMTime
   {
           NSLog("Handling image response")
           handleImageResponse(imgData, timeStamp: timeStamp)

    } else {
           NSLog("Image response failed")
    }

}

这是在控制台上打印的内容

  2019-11-02 23:47:52.342324+0530 MyApp [312:9779] Received image Optional(CMTime: {34515835925000/1000000000 = 34515.836})
  2019-11-02 23:47:52.343251+0530 MyApp[312:9779] Image response failed

在 'if let' 中删除对 CMTime 的检查可以解决问题,但是我该如何从参数字典中提取 CMTime?

  1. 我想知道在使用 Xcode 11.2 构建时突然发生了什么变化?

  2. params!["timestamp"] 正在控制台中打印 CMTime,那么 if let 赋值失败的原因是什么?

失败的是 CMTime,我试过这个:

   if let imgData = params?["image"] as? Data, let timeStamp = params?["timestamp"] as? Any {

      let time = timeStamp as! CMTime

   }

然后我崩溃了:

 Could not cast value of type 'CMTimeAsValue' (0x10ce321d8) to '__C.CMTime' (0x2355bab00).

显然时间在字典中存储为 NSValue 包装 CMTime。在这种情况下,提取时间的正确方法是

if let val = params["timestamp"] as? NSValue {
    let time = val.timeValue
    // ...
}

另见 NSValue(time:) and NSValue.timeValue