vijju
vijju

Reputation: 462

could not cast NSNULL to NSString response from API in swift

I am getting the response from an API as array of objects. So i stored that data into one variable. The response may contains the some non null values and some null values. When the time of assigning the values to any variable, i am getting the error like "Could not cast the NSNUll to NSString". If the array contains the null values then i want to replace the those error values with "space" or anything.

let parameters: Parameters = [
            "PhoneNumber":currentlevelphonenum
        ]
        Alamofire.request("https://us-central1-192704.cloudfunctions.net/Transaction/post", method: .post, parameters: parameters, encoding: JSONEncoding.default)
            .responseJSON{ //response in
                response in switch response.result {
                case .success(let JSON):
                    //print("Success with JSON: \(JSON)")
                    let response = JSON as! NSDictionary
                    //example if there is an id
                    let status:[NSObject] = response.object(forKey: "Status") as! [NSObject]
                    print("status",status)

                    let errormessage:[NSObject] = response.object(forKey: "Message") as! [NSObject]
                    print("errormessage",errormessage)



                    let contactViewController = self.storyboard?.instantiateViewController(withIdentifier: "TransactionDetails")as! TransactionDetails

                    contactViewController.sstatus = status as! [String] 
                    contactViewController.mmessage = errormessage as![String]


                    self.navigationController?.pushViewController(contactViewController, animated: true)





                case .failure(let error):
                    print("Request failed with error: \(error)")
                }
        }

I want to send the status and message arrays to next view controller with null values also. How to solve this issue? The API response like is :[failed, <null>, <null>, succeeded, succeeded, succeeded, succeeded, succeeded, <null>, succeeded, <null>, <null>, <null>, succeeded, <null>, succeeded, succeeded, succeeded, failed, failed, failed, <null>, pending, Failed, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, pending, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, pending, pending, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, <null>, pending, pending, pending, <null>, <null>, pending, pending, <null>, <null>, <null>, <null>] I want to replace the null value with space and send that array to next controller.

Upvotes: 0

Views: 90

Answers (1)

vadian
vadian

Reputation: 285260

First of all do not use NSDictionary and NSObject in Swift, use native types and key subscription

let response = JSON as! [String:Any]
//example if there is an id
let status = response["Status"] as! [Any]
print("status",status)

let errormessage = response["Message"] as! [Any]
print("errormessage",errormessage)

To change NSNull to "" use map

let mappedStatus = status.map { $0 is NSNull ? "" : $0 }

Upvotes: 2

Related Questions