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

6
  • 1
    don't cast it as string use string interpolation like "\(yourVariable)" this will not cause crash Commented Jul 17, 2018 at 5:18
  • @ Devil Decoder can you post the answer, I will accept it... Commented Jul 17, 2018 at 5:19
  • Don't force unwrap things and you will save many more crashes like this. Use guard let/if let. Commented Jul 17, 2018 at 5:23
  • @ Sharad Chauhan can you post best solution for me. Commented Jul 17, 2018 at 5:24
  • 2
    Possible duplicate of Swift: How to remove a null value from Dictionary? Commented Jul 17, 2018 at 5:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm waiting for your answer. Please respond fro me.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.