自从升级到Xcode11.2并构建和运行我的应用程序以来,Swift if let block一直报告失败,而这通常在其他情况下也能正常工作。这是失败的代码。
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检查可以解决此问题,但如何从params字典中提取CMTime?
我想知道,用Xcode11.2构建时,突然发生了什么变化?
帕拉姆斯![“timestamp”]正在控制台中打印CMTime,那么if-let分配中有什么失败?
是时候失败了,我试过:
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:)
和NSValue.timeValue
。关于ios - 如果在Xcode 11.2上使用CMTime失败,则Swift 4.2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58673994/