Red Heart
Red Heart

Reputation: 41

How to get object from array in swift

I want to get status and message from these array. I don't know how to map.

[<null>, {
    message = "Not matched";
    status = 400;
}]

This is my code

class QRScanValidAPIMapper: NSObject {
    var message: String!
    var status: Int!

    init(_ rawData: Any) {
        print(rawData)
        let data = rawData as? Dictionary<String,Any>
        self.message = data?["message"] == nil ? rawData as! String : data?["message"] as! String
        self.status = data?["status"] == nil ? 0 : data?["status"] as! Int
    }
}

Upvotes: 1

Views: 2737

Answers (1)

Amir Khan
Amir Khan

Reputation: 1318

Get the failure dictionary from Array and get the value from it. like this -

let array = [nil, ["message" : "Not matched", "status" : 400]]

for response in array {

        if let failureResponse = response {

            print(failureResponse["message"]!)
            print(failureResponse["status"]!)
        }
}

Let me know whether you are still having any issue.

Upvotes: 1

Related Questions