У меня есть следующий файл JSON:
[
{
"countryName": "Afghanistan",
"continent": "Asia",
"population": 34656032,
"currency": "Afghani"
},
{
"countryName": "Albania",
"continent": "Europe",
"population": 2876591,
"currency": "Lek"
},
{
"countryName": "Bulgaria",
"continent": "Europe",
"population": 7050034,
"currency": "Lev"
},
{
"countryName": "Finland",
"continent": "Europe",
"population": 5517887,
"currency": "Euro"
},
{
"countryName": "Iceland",
"continent": "Europe",
"population": 350710,
"currency": "Icelandic Krona"
},
{
"countryName": "Japan",
"continent": "Asia",
"population": 126672000,
"currency": "Yen"
},
{
"countryName": "Oman",
"continent": "Middle East",
"population": 4424762,
"currency": "Rial"
},
{
"countryName": "South Africa",
"continent": "Africa",
"population": 57725600,
"currency": "South African Rand"
},
{
"countryName": "Uruguay",
"continent": "South America",
"population": 3444006,
"currency": "Uruguayan Peso"
},
{
"countryName": "Venezuela",
"continent": "South America",
"population": 31568179,
"currency": "Bolivar Soberano"
}
]
После анализа его в моем проекте с использованием SwiftyJson, я бы хотел, чтобы каждая ячейка в моем tableView имела заголовок 'countryName', поэтомуследующий код:
import UIKit
class ViewController: UITableViewController {
var countries = [[String: String]]()
var numberOfCountries = 0
override func viewDidLoad() {
super.viewDidLoad()
title = "Select country"
if let jsonPath = Bundle.main.path(forResource: "Countries", ofType: "json") {
if let data = try? String(contentsOfFile: jsonPath) {
print("data: \(data)")
let json = JSON(parseJSON: data)
parse(json: json)
print("countries: \(countries)")
} else {
print("Unable to get contents of JSON file")
}
}
}
func parse(json: JSON) {
for result in json.arrayValue {
let countryName = result["countryName"].stringValue
let continent = result["continent"].stringValue
let population = result["population"].stringValue
let currency = result["currency"].stringValue
let obj = ["countryName": countryName, "continent": continent, "population": population, "currency": currency]
countries.append(obj)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let numberOfCountries = countries.count
return numberOfCountries
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)
var cellTitle = ""
for item in countries {
cellTitle = item["countryName"] as! String
print("cellTitle: \(cellTitle)")
}
cell.textLabel?.text = cellTitle
return cell
}
Однако массив просто зацикливается на десять раз, и Венесуэла выбрана в качестве заголовка для каждой ячейки tableView.
Моя консоль отладки выглядит следующим образом:
countries: [["countryName": "Afghanistan", "continent": "Asia", "population": "34656032", "currency": "Afghani"], ["countryName": "Albania", "continent": "Europe", "population": "2876591", "currency": "Lek"], ["countryName": "Bulgaria", "continent": "Europe", "population": "7050034", "currency": "Lev"], ["countryName": "Finland", "continent": "Europe", "population": "5517887", "currency": "Euro"], ["countryName": "Iceland", "continent": "Europe", "population": "350710", "currency": "Icelandic Krona"], ["countryName": "Japan", "continent": "Asia", "population": "126672000", "currency": "Yen"], ["countryName": "Oman", "continent": "Middle East", "population": "4424762", "currency": "Rial"], ["countryName": "South Africa", "continent": "Africa", "population": "57725600", "currency": "South African Rand"], ["countryName": "Uruguay", "continent": "South America", "population": "3444006", "currency": "Uruguayan Peso"], ["countryName": "Venezuela", "continent": "South America", "population": "31568179", "currency": "Bolivar Soberano"]]
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
Как я могу зациклить этот массив только один раз, выбрав значение 'countryName' в каждом случае, а затем использовать его в качестве заголовка ячейки?
Спасибо всем, ктоможет помочь.Этот сводит меня с ума!