【发布时间】: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