my Json
price = ({
maxprice = "<null>";
minprice = "<null>";
});
code
var Arr = NSArray()
Arr = result.value(forKey: "price") as! NSArray
let max = Arr.value(forKey: "maxprice")
if max == nil {
self.MaxValue = 0
self.MinValue = 0
}
else {
let maxval = (Arr.object(at: 0) as AnyObject).value(forKey: "maxprice") as! NSNumber
let min = (Arr.object(at: 0) as AnyObject).value(forKey: "minprice") as! NSNumber
self.MaxValue = maxval
self.MinValue = min
}
Error: Could not cast value of type 'NSNull' (0x102148918) to 'NSNumber' (0x10311c4a8).
You can also use is
to check for the presence of a null:
if Arr.value(forKey: "maxprice") is NSNull {
// do something with null JSON value here
}
Swift 3.0
You can get this in short line of code using ??
as like below also we can say rescue operation.
let max = Arr.value(forKey: "maxprice") as? NSNumber ?? 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With