After an Xcode update I encountered a weird problem. In my application, when I get a response from an API, I parse it manually and map it to my model. There are a lot of places in my code where I cast a JSON value to Float
with null coalescing like below:
randomVariable = jsonDict["jsonKey"] as? Float ?? 0
It used to work fine but after the update, it would always default to 0. In order to determine the cause, I tried force casting it to Float
like below
randomVariable = jsonDict["jsonKey"] as! Float
This resulted in the following error
Unable to bridge NSNumber to Float
Upon further investigation, I found out that Xcode is using Swift 3.3 (previously, it was using Swift 3.2). The solutions I found on StackOverflow revolve around casting it to NSNumber
instead of Float
. As I mentioned above, I have similar lines of code spread across my app. I was wondering if there is a way to fix this issue. Maybe using an extension of some sort?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…