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.