0

I am new of programming in swift.I want to ask that why is Per not being decoded

class HTTPUser: ObservableObject{
    func getper(value: String, completion:@escaping (Result<String, AuthenticationError>) -> Void){
        guard let url = URL(string:"http://127.0.0.1:8080/getperinfo") else{
            fatalError("URL is not defined")
        }
        var request=URLRequest(url: url)
        request.httpMethod = "GET"
        request.addValue("Bearer \(value)", forHTTPHeaderField: "Authorization")
        URLSession.shared.dataTask(with: request){
            (data, response, error) in
            
            guard let httpURLResponse = response as? HTTPURLResponse else { return; }
            if let data = data {
                do {
                    let loginResponse = try JSONDecoder().decode(Per.self, from: data)
                    DispatchQueue.main.async {
                        let changeuser = loginResponse.user
                        let changeid = loginResponse.id
                        let statusCode = httpURLResponse.statusCode
                        if statusCode == 200{
                            print(changeid!)
                            print(changeuser!)
                        }else{
                            completion(.failure(.invalidCredentials))
                        }
                    }
                } catch DecodingError.keyNotFound(let key, let context) {
                    Swift.print("could not find key \(key) in JSON: \(context.debugDescription)")
                } catch DecodingError.valueNotFound(let type, let context) {
                    Swift.print("could not find type \(type) in JSON: \(context.debugDescription)")
                } catch DecodingError.typeMismatch(let type, let context) {
                    Swift.print("type mismatch for type \(type) in JSON: \(context.debugDescription)")
                } catch DecodingError.dataCorrupted(let context) {
                    Swift.print("data found to be corrupted in JSON: \(context.debugDescription)")
                } catch let error as NSError {
                    NSLog("Error in read(from:ofType:) domain= \(error.domain), description= \(error.localizedDescription)")
                }
                return
            }

        }.resume()
    }}

this is Per of the Models

import Foundation


struct Per: Codable{
    var id: String?
    var name: String?
    var account: String?
    var picture: String?
    var age: String?
    var birth: String?
    var city: String?
    var hobby: String?
    var user: UserLogin?
}

and this is the UserLogin of the Models

import Foundation

struct UserLogin:Codable{

    var id: String?
    var account: String?
    var password: String?

}

the error is type mismatch for type Dictionary<String, Any> in JSON: Expected to decode Dictionary<String, Any> but found an array instead.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
Yian
  • 3
  • 1
  • The error message says it is an array so try an array, `decode([Per].self, from: data)` – Joakim Danielson Aug 11 '21 at 06:33
  • I tried that b4 but it still can't work.The main question is I want to fetch the user and id . – Yian Aug 11 '21 at 12:03
  • B4 is not a word, please use plain English to be sure people understand you. There is nothing in your question about fetching the user and/or id so it’s about decoding. Anyway to get to the main point, if you get that error then the suggested solution should work but we can’t be 100% sure of course since you haven’t included the json message – Joakim Danielson Aug 11 '21 at 12:16

0 Answers0