Pulling JSON from: https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD which currently returns:
{
"ask": "6746.95",
"bid": "6738.92",
"last": "6745.46",
"high": "7331.56",
"low": "6722.81",
"open": {
"hour": "7158.04",
"day": "7158.04",
"week": "7024.12",
"month": "4822.05",
"month_3": "3405.78",
"month_6": "1744.77",
"year": "719.80"
},
"averages": {
"day": "7092.52",
"week": "7113.14",
"month": "6776.50"
},
"volume": "182053.35813936",
"changes": {
"price": {
"hour": "-412.58",
"day": "-412.58",
"week": "-278.66",
"month": "1923.41",
"month_3": "3339.68",
"month_6": "5000.69",
"year": "6025.66"
},
"percent": {
"hour": "-5.76",
"day": "-5.76",
"week": "-3.97",
"month": "39.89",
"month_3": "98.06",
"month_6": "286.61",
"year": "837.13"
}
},
"volume_percent": "79.66",
"timestamp": 1510336968,
"display_timestamp": "2017-11-10 18:02:48"
}
was attempting to extract the "last" value as a double by:
if let lastValue = json["last"].double { print(lastValue) }
// did other things with the lastValue than just print.
I expected the string would convert successfully as Double? or nil if the data was not available. Using json["last"].doubleValue works.
The typecast failed and my "else" segment of code ran instead.
List the software versions you're using:
Xcode Default
.)Please also mention which package manager you used and its version. Delete the
other package managers in this list:
pod --version
in Terminal)Looking at your code I think this is because public var numberValue: NSNumber
has a .string case where public var number: NSNumber?
does not. Adding the following code fixed this problem:
//Optional number
public var number: NSNumber? {
get {
switch self.type {
case .string:
let decimal: NSDecimalNumber? = NSDecimalNumber(string: self.object as? String)
if decimal == NSDecimalNumber.notANumber { // indicates parse error
return nil
}
return decimal
case .number:
return self.rawNumber
case .bool:
return NSNumber(value: self.rawBool ? 1 : 0)
default:
return nil
}
}
set {
self.object = newValue ?? NSNull()
}
}
Forked the code, added the fix and provided some new unit tests, created pull request.
Sounds like this issue is still not merged into v4.1.0 as i'm facing the exact same problem.
Any update on that ?
I'm not sure how they decide what to merge vs not.
Same problem with .bool and .int
Most helpful comment
Forked the code, added the fix and provided some new unit tests, created pull request.