Whenever you try to get responses from API, you get the JSON response in most cases and this error occurs all the time while getting the value from JSON array or dictionary as Int or as NSNumber.
In many cases (NOT ALL), the reason of the error is the JSON data we get is a String (Note: NSTaggedPointerString is a subclass of NSString).
So what we can do is to convert String to Double.
Like this:
// check dict["dummy"] is a String first
if let receivedData = (dict["dummy"]).doubleValue {
// add more code in case the condition is true
}
else {
// add more code in case the condition is false
}
OR
// check dict["dummy"] is a String first
if let receivedData = (dict["dummy"] as? NSString)?.doubleValue {
// add more code in case the condition is true
}
else {
// add more code in case the condition is false
}
Both of those solutions work.
It is really helpful when you want to compare the received data or just check the value like the following example.
let receivedData = (results["data"]!).doubleValue
if (receivedData == 0){
self.label.text = "Nothing seem to be added yet!"
}
app Apple error iOS iPhone NSNumber solution