James
James

Reputation: 15

Integer overflows when stored into 'Int Phone Number

In xcode 8, I am getting an error when I try to archive. This is the code:

@IBAction func dialNumber(_ sender: AnyObject) {

    if let url = URL(string: "tel://\(8708382937)") {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }
}

On the line that begins with "if let..." I am getting this error: Integer literal '8708382937' overflows when stored into 'Int'

Upvotes: 0

Views: 419

Answers (2)

Rob Napier
Rob Napier

Reputation: 299355

This is expected and a correct error. Why are you putting the number in \()? That evaluates it as Swift. As Swift that's a literal number, which is too big to fit in an Int. You almost certainly mean this:

"tel://8708382937"

Or more sensibly:

"tel:8708382937"

(The slashes are specifically part of the HTTP URL-scheme. They are not a general part of URLs and do not belong on tel URLs.)

Upvotes: 4

picciano
picciano

Reputation: 22701

There's no reason not to just use a string literal for the URL.

if let url = URL(string: "tel://8708382937") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Upvotes: 0

Related Questions