I am saving all data and the code is written in the separate class. And it shows an error i.e. cannot convert value of type 'Int' to expected argument type 'String'. Please help me to resolve this.
The following is the Class where i saved all data:
class FetchedLocations
{
var id: Int
var companyID: Int
var username: String
var name: String
var imei: String
var created_at: String
var updated_at: String
var module_id: Int
var location_address: String?
var guard_name: String?
var guard_contact: String?
var device: Int?
var expire_on: Int?
var status: Int
init(id: Int, companyID: Int, username: String, name: String, imei: String, created_at: String, updated_at: String, module_id: Int, location_address: String, guard_name: String, guard_contact: String, device: Int, expire_on: Int, status: Int) {
self.id = id
self.companyID = companyID
self.username = username
self.name = name
self.imei = imei
self.created_at = created_at
self.updated_at = updated_at
self.module_id = module_id
self.location_address = location_address
self.guard_name = guard_name
self.guard_contact = guard_contact
self.device = device
self.expire_on = expire_on
self.status = status
}
}
And this is the ViewController.swift file:
for eachDepartment in json["locations"]! {
let eachData = eachDepartment as! [String: Any]
let id = eachData["id"] as! Int
let companyID = eachData["company_id"] as! Int
let username = eachData["username"] as! String
let locName = eachData["name"] as! String
let imei = eachData["imei"] as! String
let createdDate = eachData["created_at"] as! String
let updatedDate = eachData["updated_at"] as! String
let moduleID = eachData["module_id"] as! Int
let locationAddress = eachData["location_address"] as! String
let guardName = eachData["guard_name"] as! String
let guardContact = eachData["guard_contact"] as! String
//let device = eachData["device"] as! Int
let device = 0
//let expireOn = eachData["expire_on"] as! Int
let expireOn = 0
//let status = eachData["status"] as! Int
let status = eachData["status"] as! Int
Date.formatter(createdDate: createdDate)
//Error encountered in this line//
FetchedLocations.init(id: id, companyID: companyID, username: username, name: locName, imei: imei, created_at: createdDate, updated_at: updatedDate, module_id: moduleID, location_address: locationAddress, guard_name: guardName, guard_contact: guardContact, device: device, expire_on: expireOn, status: status)
}
Error encountered:
Cannot convert value of type 'Int' to expected argument type 'String'
Intrather thanString. Set a breakpoint and use the debugger. Use theCodableprotocol in Swift 4. Its error messages are still more meaningful.as!is dangerous. Useguard let eachData = eachDepartment as? [String: Any], ... else { continue }to avoid crashes. Obviously you still need to find which of theseas! Intlines is meant to beas! String.