简体   繁体   中英

Swift Error: keyNotFound No value associated with key CodingKeys

Trying to parse a JSON response and running into this error.

keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "id", intValue: nil) ("id").", underlyingError: nil))

I'm not making heads or tails of why it's looking for a CodingKey in the first place for id since the response contains an id.

Here's the JSON response from the server:

{
    "answer": {
        "id": 6,
        "title": "Here is the postman API answer",
        "ownerId": 1
    }
}

Here's the struct I'm trying to merge it into:

// MARK: - Answer
struct Answer: Codable {
    let id: Int
    let title: String
    let ownerID: Int
    let questionID, projectID: Int?

    enum CodingKeys: String, CodingKey {
        case id, title
        case ownerID = "ownerId"
        case questionID = "questionId"
        case projectID = "projectId"
    }
}

and here's the function:

func createAnswer(questionId: Int, title: String, completed: @escaping(Result<Answer, AuthenticationError>) -> Void){
guard let url = URL(string: "https://mywebsitehere.com/api") else {
    completed(.failure(.custom(errorMessage:"URL unavailable")))
    return
}

guard let Accesstoken = UserDefaults.standard.string(forKey: "access-token") else { return }
guard let client = UserDefaults.standard.string(forKey: "client") else { return }
guard let uid = UserDefaults.standard.string(forKey: "userEmail") else { return }

let body = AnswerCreateBody(title: title)

var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue(Accesstoken, forHTTPHeaderField: "access-token")
        request.addValue(client, forHTTPHeaderField: "client")
        request.addValue(uid, forHTTPHeaderField: "uid")
        request.addValue("Bearer", forHTTPHeaderField: "Tokentype")
        request.addValue("keep-alive", forHTTPHeaderField: "Connection")
        request.httpBody = try? JSONEncoder().encode(body)

URLSession.shared.dataTask(with: request) { (data, response, error) in
    
    if let response = response as? HTTPURLResponse {
        
        let statusCode = response.statusCode
        
        if (statusCode != 200){
            print(statusCode)
            completed(.failure(.custom(errorMessage: "Authentication Failed.  Need to login again")))
        }

        guard let data = data, error == nil else { return }
        do {
         let answerCreateResponse = try JSONDecoder().decode(Answer.self, from: data)
         completed(.success(answerCreateResponse))
            print(answerCreateResponse)
        } catch {
            print(error)
        }
    }
}.resume()
}

Why am I getting this error and how do I correct it?

This is a common mistake. You are ignoring the root object, the dictionary with key answer which of course doesn't have a key id

Add this struct

struct Root: Decodable {
    let answer : Answer
}

and decode

let answerCreateResponse = try JSONDecoder().decode(Root.self, from: data).answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM