Если вы не хотите вводить ссылку, вот как выглядит структура JSON:
Код: Выделить всё
{
"items":[
{
"categorie":"Fruits",
"id_categorie":"1"
},
{
"categorie":"Animals",
"id_categorie":"2"
},
{
"categorie":"Juices",
"id_categorie":"3"
},
{
"categorie":"Vegetables",
"id_categorie":"4"
},
{
"categorie":"Alcohol",
"id_categorie":"5"
},
{
"categorie":"Desserts",
"id_categorie":"6"
}
]
}
По сути, я хочу распечатать категории и сохранить каждую из них в переменных (потому что я буду нужно переместить переменные между экраны)
Вот как выглядит мой код:
Код: Выделить всё
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://proyecto-idts6.epizy.com/models/getCategorias.php"
getData(from: url)
//Here is where i want to storage the variables from the JSON
}
private func getData(from url: String) {
let task = URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { data, response, error in
guard let data = data, error == nil else {
print("something went wrong")
return
}
do {
let result = try JSONDecoder().decode([ResultItem].self, from: data)
print(result)
}
catch {
print("failed to convert\(error)")
}
})
task.resume()
}
}
struct Response: Codable {
let items: [ResultItem]
}
struct ResultItem: Codable {
let categorie: String
}
categorie1=("категория 1, вызываемая из JSON"), categorie2=("категория 2, вызываемая из JSON"), categorie3=("категория 3, вызываемая из JSON"),...
Подробнее здесь: https://stackoverflow.com/questions/723 ... i-properly
Мобильная версия