简体   繁体   English

为什么我的 Int64 值为零?

[英]Why am I getting nil for the Int64 value?

I run this code and get clients names correctly, but when it comes to integers (number of awards, number of cars, number of dogs), it seems that I am not getting anything from the API (the '?? 0' part kicks in and returns nil).我运行此代码并正确获取客户名称,但是当涉及整数(奖项数量、汽车数量、狗数量)时,我似乎没有从 API 得到任何东西('?? 0' 部分踢in 并返回 nil)。

Here's my code:这是我的代码:

func getAllClients() {
    AF.request("xxxxxxxxxxxxx", headers: headers).responseJSON {response in
        let result = response.value
        var allCli: [ClientsData] = []
        if result != nil {
            let dataDictionary = result as! [Dictionary <String, AnyObject>]
            for clientsData in dataDictionary {
                let cllstname = clientsData["cllstname"] as? String ?? "Error"
                let noofawards = clientsData["noofawards"] as? Int64 ?? 0
                let noofcars = clientsData["noofcars"] as? Int64 ?? 0
                let noofdogs = clientsData["noofdogs"] as? Int64 ?? 0
                let clientsObject = ClientsData (cllstname: cllstname, noofawards: noofawards, noofcars: noofcars, noofdogs: noofdogs)

                allCli.append(clientsObject)
            }
        }
        self.allClients = allCli.sorted(by: { $0.noofawards > $1.noofawards })
    }
}

However, if I do但是,如果我这样做

print(clientsData)

I get a nice and full response in the debug section.我在调试部分得到了很好的完整响应。 All the numbers are there, so API is sending, and I am receiving.所有的数字都在那里,所以 API 正在发送,我正在接收。

Why am I getting nil results, and how to fix this?为什么我得到 nil 结果,以及如何解决这个问题?

You should look at the raw JSON.您应该查看原始 JSON。 For example, grab the data and例如,抓取data

let string = response.data.flatMap { String(data: $0, encoding: .utf8) }
print(string ?? "Unable to get String representation of the data")

I'm wagering that the numbers will be in quotes meaning that they're really string representations of numeric values, not actually numbers:我打赌这些数字将用引号引起来,这意味着它们实际上是数值的字符串表示形式,而不是实际的数字:

[{"noofcars": "2", "cllstname": "Smith", ...

Note, I'm asking you to look at the raw JSON, not just the result which has already been decoded.请注意,我要求您查看原始 JSON,而不仅仅是已经解码的result We need to see how the raw JSON represented these numeric values.我们需要了解原始 JSON 如何表示这些数值。

Let's assuming for a second that my supposition is correct, that you're getting these values as strings.让我们假设我的假设是正确的,您将这些值作为字符串获取。 Clearly, the correct solution is that the web service should send numeric value as numbers, not as strings.显然,正确的解决方案是 web 服务应该将数值作为数字而不是字符串发送。 But you want to work with the existing JSON, you first get the String representation for numofcars (etc.), and then convert that to an numeric value.但是您想使用现有的 JSON,首先获取numofcars (等)的String表示形式,然后将其转换为数值。 For example:例如:

AF.request("xxxxxxxxxxxxx", headers: headers).responseJSON { response in
    guard let dictionaries = response.value as? [[String: Any]] else { return }

    self.allClients = dictionaries.compactMap { dictionary -> ClientsData? in
        let cllstname = dictionary["cllstname"] as? String ?? "Error"
        let awards = (dictionary["noofawards"] as? String).flatMap { Int($0) } ?? 0
        let cars = (dictionary["noofcars"] as? String).flatMap { Int($0) } ?? 0
        let dogs = (dictionary["noofdogs"] as? String).flatMap { Int($0) } ?? 0
        return ClientsData(cllstname: cllstname, noofawards: awards, noofcars: cars, noofdogs: dogs)
    }
    .sorted { $0.noofawards > $1.noofawards }
}

FWIW, please forgive the refactoring above, as I would recommend map or compactMap rather than building an empty array and appending values to it. FWIW,请原谅上面的重构,因为我会推荐mapcompactMap而不是构建一个空数组并向其附加值。 It's just a bit more concise它只是更简洁一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM