Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type 'Int64?' to expected argument type 'Int'

When I try to pass job?.id (an Int64) as Int parameter ( while I know it's not that big ), swift compiler prompts with this error, I tried a couple of ways to cast it, but had no success :

Cannot convert value of type 'Int64?' to expected argument type 'Int'

My code :

Job.updateJobStatus(token: tok, jobId: job?.id, status:JobSatus.canceled) { (result, error) in
        if result != nil
        {

        }
        else if error != nil
        {

        }
    }
like image 873
AVEbrahimi Avatar asked Nov 27 '17 15:11

AVEbrahimi


2 Answers

You can easily go like:

let int64Value: Int64 = 123456
let intValue = NSNumber(value: int64Value).intValue
like image 51
alitosuner Avatar answered Sep 25 '22 21:09

alitosuner


This is related to https://stackoverflow.com/a/32793656/8236481

Swift 3 introduces failable initializers to safely convert one integer type to another. By using init?(exactly:) you can pass one type to initialize another, and it returns nil if the initialization fails. The value returned is an optional which must be unwrapped in the usual ways.

Int(exactly: yourInt64)
like image 24
Oxthor Avatar answered Sep 24 '22 21:09

Oxthor