如何快速从数组中获取对象

时间:2019-04-01 04:17:32

标签: arrays swift

我想从这些阵列获取状态和消息。 我不知道如何映射。

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

这是我的代码

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

1 个答案:

答案 0 :(得分:1)

failure获取Array字典并从中获取值。像这样-

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

for response in array {

        if let failureResponse = response {

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

让我知道您是否仍然有任何问题。