0

I'm getting this error :

Could not cast value of type 'NSNull' (0x1114e4850) to 'NSString' (0x11065d2a8).

//These are my arrays
var snoArray:[Int] = []
var quote_idArray:[String] = []
var quote_amountArray:[String] = []
var dateArray:[String] = []
var timeArray:[String] = []
var perkilometerArray:[String] = []
var pertonArray:[String] = []

//My JSON response is like this.
["Response": {
Array =     (
            {
        date = "25-05-2018";
        perkilometer = 2;
        perton = "<null>";
        "quote_amount" = 1234;
        "quote_id" = 5b080f8aa082c;
        sno = 1;
        time = "18:58";
    },
            {
        date = "25-05-2018";
        perkilometer = 346;
        perton = "<null>";
        "quote_amount" = 230000;
        "quote_id" = 5b080ed57aa34;
        sno = 2;
        time = "18:55";
    }
);
"error_code" = 0;
message = SUCCESS;
status = SUCCESS;
}, "count": 2]

//I'm catching response like this...
if status == "SUCCESS" {
   if let array = res!["Array"] as? Array<Dictionary<String, Any>> {
      //    print(array.count)
      self.snoArray = array.map { $0["sno"]! } as! [Int]
      self.quote_idArray = array.map { $0["quote_id"]! } as! [String]
      self.quote_amountArray = array.map { $0["quote_amount"]! } as! [String]
      self.dateArray = array.map { $0["date"]! } as! [String]
      self.timeArray = array.map { $0["time"]! } as! [String]
      self.perkilometerArray = array.map { $0["perkilometer"]! } as! [String]
      //But here I'm getting null values and app crashed
      self.pertonArray = array.map { $0["perton"]! } as! [String]
   }
} 

//When I write like this, it's printing : [<null>, <null>]
let perTon = array.compactMap({ $0 ["perton"]})
 print(perTon) // Output: [<null>, <null>]

I referred this link : https://useyourloaf.com/blog/swift-non-nil-values-in-an-array-of-optionals/

Here i want to remove null values from self.pertonArray and want to print empty self.pertonArray([ , ]).

If suppose I get response like this how to solve this problem.

["Response": {
Array =     (
            {
        date = "25-05-2018";
        perkilometer = 2;
        perton = "something";
        "quote_amount" = 1234;
        "quote_id" = 5b080f8aa082c;
        sno = 1;
        time = "18:58";
    },
            {
        date = "25-05-2018";
        perkilometer = 346;
        perton = "<null>";
        "quote_amount" = 230000;
        "quote_id" = 5b080ed57aa34;
        sno = 2;
        time = "18:55";
    }
);
"error_code" = 0;
message = SUCCESS;
status = SUCCESS;
}, "count": 2] 

How to solve this problem....

Naresh
  • 16,698
  • 6
  • 112
  • 113

2 Answers2

3

There are a lot of ways to remove nil from JSON when binding. But here is one according to your code.

if status == "SUCCESS" {
   if let array = res!["Array"] as? Array<Dictionary<String, Any>> {
      //    print(array.count)
      self.snoArray = array.map { ($0["sno"] as? Int) ?? 0 }
      self.quote_idArray = array.map { ($0["quote_id"] as? String) ?? "" }
      self.quote_amountArray = array.map { ($0["quote_amount"] as? String) ?? "" }
      self.dateArray = array.map { ($0["date"] as? String) ?? "" }
      self.timeArray = array.map { ($0["time"] as? String) ?? ""} 
      self.perkilometerArray = array.map { ($0["perkilometer"] as? String) ?? "" }

      // You get error here with null values
      // here is how it's fixed
      self.pertonArray = array.map { ($0["perton"] as? String) ?? "" }
   }
} 

It's a good practice to not use Force Unwrapping when binding json because you will never be sure there will be no null or incorrect data (You expect Int but the value is String as sent from Server).

Cosmos Man
  • 593
  • 3
  • 15
1

use string interpolation to parse value

like in your response

you want to get perton

let value = ((responce as! Array)[indexofarray] as! Dictionary<string,any>)["perton"]
let perton = "\(value ?? "")"

this will return something for first index and for second index it will return <null> in string

Devil Decoder
  • 966
  • 1
  • 10
  • 26