6

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'

3
  • 2
    The error is very clear: One of the values is (actually) Int rather than String. Set a breakpoint and use the debugger. Use the Codable protocol in Swift 4. Its error messages are still more meaningful. Commented Apr 21, 2018 at 8:39
  • 1
    this part of your code is completely fine and there isn't any problem in this, check the rest of your code Commented Apr 21, 2018 at 8:50
  • Most of the time using as! is dangerous. Use guard let eachData = eachDepartment as? [String: Any], ... else { continue } to avoid crashes. Obviously you still need to find which of these as! Int lines is meant to be as! String. Commented Apr 21, 2018 at 9:47

2 Answers 2

13

Try this

let a: Int = 5
let str = String(a)

You can use codable to parse your data https://medium.com/xcblog/painless-json-parsing-with-swift-codable-2c0beaeb21c1

https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

Sign up to request clarification or add additional context in comments.

Comments

1

Just use another variable.

For example, try:

let stringFromIntToString = String(yourIntVariable)

which should work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.