5
  1. I want to check that the maxprice is equal to "" this or not if it's "" then maxprice will become maxprice = 0

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).

Meloman
  • 3,558
  • 3
  • 41
  • 51
user8198079
  • 53
  • 1
  • 1
  • 5
  • if you want string as "" (empty) then you can use string.isEmpty (Check this question https://stackoverflow.com/questions/24133157/check-empty-string-in-swift) – Kamaldeep singh Bhatia Sep 18 '17 at 06:28
  • i want to check that "" – user8198079 Sep 18 '17 at 06:31
  • 1
    If you mean literally the string `` you can just check `if max == ""`. If you mean the Swift value nil, you should use a null coalescing operator: `MaxValue = blahblah ?? 0` – charles-allen Sep 18 '17 at 06:34
  • Actually throwing any non-numeric string into `as? Int` will result in `nil`, so that's probably the way to go. – charles-allen Sep 18 '17 at 06:45
  • 1
    This is Swift. Do not use `NSArray`, this avoids a horrible syntax like `... as AnyObject).value(forKey: "maxprice")` and do not use `valueForKey` unless you can explain why you need KVC. And why `NSNumber`? There are native types (`Int`, `Double`) in Swift – vadian Sep 18 '17 at 06:46

3 Answers3

12

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
}
AshokPolu
  • 645
  • 5
  • 12
  • Is this ObjectiveC style? I think in Swift you want `if Blah == nil { ... }`, or better yet, unbox the optional at the same time using `if let unboxed = Blah { // use unboxed }` – charles-allen Sep 18 '17 at 06:43
  • this is checking the value is null or not in swift. – AshokPolu Sep 18 '17 at 06:44
3

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
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
2

This is the Swift way.

Use optional bindings and do not use objectivecish NSArray, valueForKey and NSNumber

You need to check

  • if the value for key price is a Swift array of dictionaries ([[String:Double]]). The distinct type avoids the extra check for NSNull
  • if the array is not empty.

Assuming minprice and maxprice are supposed to be Double values, if not replace Double with Int

if let priceArray = jsonDict["price"] as? [[String:Double]],
    let price = priceArray.first {
    minValue = price["minprice"] ?? 0.0
    maxValue = price["maxprice"] ?? 0.0
} else {
    minValue = 0.0
    maxValue = 0.0
}
vadian
  • 274,689
  • 30
  • 353
  • 361