【问题标题】:Why array returns nil after appending to it elements?为什么数组在附加元素后返回 nil?
【发布时间】:2016-05-19 09:11:29
【问题描述】:

我正在尝试使用 JSON 作为 MVC 模型,为此我做到了:

// Country.swift
import SwiftyJSON

class Country {
     var code: String!
     var dialCode: Int!
     var name: String!

     init(json: JSON) {
          for i in 0...json["countries"].count - 1 {
             if let code = json["countries"][i]["code2"].string, dialCode = json["countries"][i]["dialCode"].string, name = json["countries"][i]["name"].string {
                 self.code = code
                 self.dialCode = Int(dialCode)
                 self.name = name
             }
         }
     }
 }

稍后在我的 ViewController 中我会这样做:

var countries = [Country]()

Alamofire.request(.POST, "\(property.host)\(property.getCountryList)", parameters: parameters, encoding: .JSON).responseJSON { response in
    do {
        let json = JSON(data: response.data!)
        countries.append(Country(json: json))
    } catch _ {

    }   
}

但是我有一个问题。当我在 Country.swift 文件中打印值时,我得到了结果,但是当我 print(countries) 它返回我 [Project.Country] 并且 count 返回 1。有什么问题?我做错了什么?

【问题讨论】:

  • 尝试打印 print(countries[0]);
  • @KishoreKumar 同样的事情

标签: ios json swift model-view-controller swifty-json


【解决方案1】:

除非我误解了这不是你想要的行为吗?

countries 是一个Project.Country 的数组,它通过打印[Project.Country](一个包含你的类的一个实例的数组)来表示。没有问题。如果您想证明该数组包含 Project.Country,您应该打印该类的属性之一:print(countries.first.name)

编辑:问题是您将国家/地区的JSON 数组传递给单个 init 方法,该方法只是为每个国家/地区设置其自身的属性,而不是为每个国家/地区创建一个实例。因此,您只返回了一个实例

【讨论】:

  • 是的,你是对的。另外,我可以使用countries[0].name,但使用起来不舒服。如何获取国家/地区数组计数?当我尝试打印 countries.count 它返回我 1
  • 我猜您希望通过您的请求返回多个国家/地区?在您的 Project.Country 初始化程序中,您只在 for 循环中创建一个实例,因为您为同一个实例设置了一次变量 - 而不是创建多个!
  • 是的。有246个国家。所以,我期待246
  • 这里for i in 0...json["countries"].count - 1 {?不,它返回给我246
  • 您正在使用for 循环,是的,但只调用一次init。您将JSON 国家/地区数组传递给单个init,该init 循环遍历数组中的每个国家并将单个实例的属性设置为这些值。因此,您只剩下一个 Country 实例,它的属性已更改 246 次。您需要在 init 外使用 for 循环,而在 for 循环内调用 init
【解决方案2】:

您的问题是您正在将国家/地区数组传递给 init 方法,该方法仅在您必须像此处那样执行时调用一次

class Country {
    var code: String!
    var dialCode: Int!
    var name: String!

    init(json: JSON) {
            if let code = json["code2"].string, dialCode = json["dialCode"].string, name = json["name"].string {
                self.code = code
                self.dialCode = Int(dialCode)
                self.name = name
            }
    }
}

在这里循环

  Alamofire.request(.POST, "", parameters: nil, encoding: .JSON).responseJSON { response in

                if let jsonResponse = response.result.value{
                    let json = JSON(jsonResponse)

                    for countriesJSON in json["countries"].arrayValue{
                        self.countries.append(Country(json: countriesJSON))
                    }

                    print(self.countries.count)
                }
            }

【讨论】:

    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2014-11-11
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多