its my model. quicktyper site translated my model
import Foundation
struct User: Codable {
let customerID, companyID: Int
let firstName, lastName, phone, email: String
let passwordHash, passwordSalt: JSONNull?
let isStatus: Bool
}
struct Data : Codable{
let custommodel : [User]
}
class JSONNull: Codable, Hashable {
public static func == (lhs: JSONNull, rhs: JSONNull) -> Bool {
return true
}
public var hashValue: Int {
return 0
}
public init() {}
public required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if !container.decodeNil() {
throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}
its my api request
import Foundation
enum Verierror: Error{
case noDatAvailable
case canNotProcessData
}
struct verirequest{
let resource : URL
//example url. this is not the truth
let resoucreString = ""
init() {
guard let resourceurll = URL(string: resoucreString) else {fatalError()}
self.resource = resourceurll
}
func getveri(completion: @escaping(Result<[User],Verierror>) -> Void){
let datatask = URLSession.shared.dataTask(with: resource) { data, _, _ in
print(String(data: data!, encoding: .utf8))
guard let jsondata = data else{
completion(.failure(.noDatAvailable))
return
}
do{
let decoder = JSONDecoder()
let veriresponse = try decoder.decode(Data.self, from: jsondata)
let details = veriresponse.custommodel
completion(.success(details))
}catch{
completion(.failure(.canNotProcessData))
}
}
datatask.resume()
}
}
My data is coming from API, but my data cannot be processed. I get a "cannotprocesdata" error. I was able to process at first but now I can't.
I translated my model to quictyper.com. At the same time, I get the error "Cannot process data" in the output.
There is no problem in api, I can test it in postman. Could the problem be with my model or elsewhere?. please Help me
its my view controller
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var listveri = [User]()
override func viewDidLoad() {
super.viewDidLoad()
tableview.dataSource = self
tableview.delegate = self
let verirequest = verirequest()
verirequest.getveri { [weak self] (result) in
switch result{
case.failure(let error):
print(error)
case.success(let data):
self?.listveri = data
DispatchQueue.main.async {
self?.tableview.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listveri.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.namelabel.text = listveri[indexPath.row].firstName
cell.surnamelabel.text = listveri[indexPath.row].lastName
cell.emaillabel.text = listveri[indexPath.row].email
return cell
}
}
CodePudding user response:
try the following structs :
struct Response: Codable {
let data: [User] // <--- here
let message: String?
let success: Bool
}
struct User: Codable {
let customerID, companyID: Int
let firstName, lastName, phone, email: String
let passwordHash, passwordSalt: String?
let isStatus: Bool
}
and decode the server response like this:
let veriresponse = try decoder.decode(Response.self, from: jsondata)