Я пытался проанализировать файл JSON, который выглядит следующим образом:
Код: Выделить всё
success({
data = {
id = "eb259a9e-1b71-4df3-9d2a-6aa797a147f6";
nickname = joeDoe;
options = {
avatar = avatar1;
};
rooms = "";
};
Выдает ошибку, которая выглядит следующим образом:
Код: Выделить всё
keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"id\", intValue: nil) (\"id\").", underlyingError: nil))
Код: Выделить всё
import Foundation
struct userModel: Codable {
let id: String
let nickname: String
let options: options
let rooms: String
enum CodingKeys: String, CodingKey {
case id = "id"
case nickname = "nickname"
case options = "options"
case rooms = "rooms"
}
}
struct options: Codable {
var avatar: String?
enum CodingKeys: String, CodingKey {
case avatar = "avatar"
}
}
Код: Выделить всё
func postUser(){
AF.request("http://test.com/", method: .post, parameters: user).responseJSON {response in
guard let itemsData = response.data else {
print("test1")
return
}
do {
print("hallo!")
let decoder = JSONDecoder()
print("")
print(itemsData)
print("")
print(response.description)
let items = try decoder.decode(userModel.self, from: itemsData)
print(items)
DispatchQueue.main.async {
print("test2")
}
} catch {
print(error)
print("test3")
}
}
Подробнее здесь: https://stackoverflow.com/questions/679 ... st-request
Мобильная версия