Getting error : - The data couldn’t be read because it isn’t in the correct format in Swift using Alamofire and Codable

Viewed 305

I am calling api using alamofire and codable . And i am getting api response succesfully. But when i am getting extra parameter in this api i am getting error like "The data couldn’t be read because it isn’t in the correct format".

My code is here

My Model class is:

import Foundation


struct SingleUserProfile: Codable {
let result: [SingleProfileResult]?
let success: Int?
let message: String?

enum CodingKeys: String, CodingKey {
    case result = "Result"
    case success = "Success"
    case message = "Message"
}
}


struct SingleProfileResult: Codable {
let userID: Int?
let firstName: String?
let location: String?
let state, about: String?
let totalScore: Int?
let sun, rising, moon: String?
let ego, communication, birthStar, sexual: Int?
let intellectual, temperament, emotional, healthGenetic: Int?
let profilePic: String?
let numberOfPhotos: Int?
let imagelist: [UserImagelist]?
let verified: Int?
let dateOfBirth, timeOfBirth: String?
let age, isLastPage: Int?

enum CodingKeys: String, CodingKey {
    case userID = "User_Id"
    case firstName = "First_Name"
    case location = "Location"
    case state = "State"
    case about = "About"
    case totalScore = "TotalScore"
    case sun
    case rising = "Rising"
    case moon = "Moon"
    case ego = "Ego"
    case communication = "Communication"
    case birthStar = "Birth_star"
    case sexual = "Sexual"
    case intellectual = "Intellectual"
    case temperament = "Temperament"
    case emotional = "Emotional"
    case healthGenetic = "Health_Genetic"
    case profilePic = "Profile_Pic"
    case numberOfPhotos = "NumberOfPhotos"
    case imagelist, verified
    case dateOfBirth = "DateOfBirth"
    case timeOfBirth = "TimeOfBirth"
    case age = "Age"
    case isLastPage = "IsLastPage"
}
}


struct UserImagelist: Codable {
let userID: Int?
let imagePath, photoID: String?

enum CodingKeys: String, CodingKey {
    case userID = "User_Id"
    case imagePath = "Image_path"
    case photoID = "Photo_Id"
}

var getFullURL : String{

    return BASE_URL + (imagePath ?? "")
}
}

In my Constant class

let SINGLE_USER_PROFILE_DETAILS = URL_BASE + GET_USER_PROFILELIST


typealias singleUserProfileDetailsCompletion = (SingleUserProfile?) -> Void

In my Api Request class

 //MARK:- Get profile details for

func getSingleUseProfileDetails(currentUserId: Int, accessToken: String, completion: @escaping singleUserProfileDetailsCompletion) {

    guard let url = URL(string: "\(SINGLE_USER_PROFILE_DETAILS)?UserID=\(currentUserId)")  else{return}
    var urlRequest = URLRequest(url: url)
    urlRequest.httpMethod = HTTPMethod.get.rawValue

    urlRequest.addValue(accessToken, forHTTPHeaderField: "Authorization")


    Alamofire.request(urlRequest).responseJSON { (response) in
        if let error = response.result.error {
            debugPrint(error)
            completion(nil)
            return
        }

        guard let data = response.data else { return completion(nil)}

        Common.sharedInstance().printURLRequestParameter(url: url, urlRequest: urlRequest, accessToken: accessToken)
        Common.sharedInstance().printRequestOutput(data: data)

        let jsonDecoder = JSONDecoder()
        do {
            let person = try jsonDecoder.decode(SingleUserProfile.self, from: data)

            completion(person)
        } catch {
            debugPrint(error)
            completion(nil)
        }
    }


}

The code is working fine. But if new extra parameter is coming i am getting error like "The data couldn’t be read because it isn’t in the correct format". How to write the model class, so that if new data will come, it will handle the situation automatically

Please help me to resolve the issue.

0 Answers
Related