我是iOS swift3的初学者。实际上我在从Json创建我自己的模型时遇到了一个问题。我的Json有三个数组,分别是CountryList,CityList和PortList。 CountryList具有countryID和countryName。 CityList有countryId,cityId和cityName。 PortList有countryId,cityId和portName。现在,我希望在tableView中显示数据,该表具有基于城市的国家和城市的国家和城市。我想创建一个模型,该模型在该国家/地区拥有国家和城市,在该城市拥有港口。如果有人帮助我,我将非常感激。 谢谢.. 这是我到目前为止所写的代码......
struct Country {
var countryId: String!
var countryName: String!
init(countryId: String, countryName: String){
self.countryId = countryId
self.countryName = countryName
}}
struct City {
var countryId: String!
var cityId : String!
var cityName: String!
init(countryId: String, cityId: String, cityName: String){
self.countryId = countryId
self.cityId = cityId
self.cityName = cityName
}}
struct Ports {
var countryId: String!
var cityId : String!
var portName: String!
init(countryId: String, cityId: String, portName: String){
self.countryId = countryId
self.cityId = cityId
self.portName = portName
}}
我创建了三个结构来保存我从Json获得的数据。 我在viewController中的代码看起来像这样。
class ViewController: UIViewController {
var countriesList = [Country]()
var citiesList = [City]()
var portsList = [Ports]()
var cities = [String]()
override func viewDidLoad() {
super.viewDidLoad()
getData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func getData(){
let url = URL(string: "http://54.186.73.184:8081/api/v0/portlist/search/1")
var request = URLRequest(url: url!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error == nil{
do{
let data = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! [String: AnyObject]
// print(data)
let countriesData = data["country_list"] as! [[String: AnyObject]]
//print(countriesData)
for ids in countriesData{
let id = ids["_id"] as! [String: AnyObject]
let cId = id["_id"] as! String
let country = id["country"] as! String
// self.countries.append(country)
let countrydata = Country(countryId: cId, countryName: country)
self.countriesList.append(countrydata)
for country in self.countriesList{
let cId = country.countryId
let cityList = self.getAllCities(withCountryId: cId!)
}
}
let cityData = data["city_list"] as! [AnyObject]
for ids in cityData{
let id = ids["_id"] as! [String: AnyObject]
// print(id)
let countryId = id["country_id"] as! String
// print(countryId)
let cityId = id["_id"] as! String
// print(cityId)
let city = id["city"] as! String
// print(city)
let cityData = City(countryId: countryId, cityId: cityId, cityName: city)
self.citiesList.append(cityData)
// print(self.citiesList)
}
let portData = data["port_list"] as! [AnyObject]
for ids in portData{
let id = ids["_id"] as! [String: AnyObject]
// print(id)
let countryId = id["country_id"] as! String
// print(countryId)
let cityId = id["city_id"] as! String
// print(cityId)
let port = id["ports"] as! String
// print(port)
let portData = Ports(countryId: countryId, cityId: cityId, portName: port)
self.portsList.append(portData)
// print(self.portsList)
}
}catch{
print(error)
}
}
}
task.resume()
}
func getAllCities(withCountryId id: String) -> [AnyObject]{
// print("citiesList---------\(self.citiesList)")
for city in self.citiesList{
if (city.countryId == id){
self.cities.append(city.cityName)
}
}
return [self.cities as AnyObject]
}}
但我得到空城列表。 希望有人找到创建符合我要求的模型的想法。 谢谢..