0

I'm using Xcode 7 Beta 3 and reading through Swift 2.2 document. I'm trying to compile this example found in the Basics section of the document:

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)

It is supposed to convert a string into an optional int. However Xcode gives the error:

Cannot call value of non-function type 'int'

I was working on Xcode 7.2.1, then I knew that Swift 2.2 is packaged with Xcode 7.3 Beta 3, so I downloaded that to try, but the same error happens. So, is the document wrong? and how to achieve the string into int conversion?

1 Answer 1

2

There is nothing wrong with your code, I tested and ran your exact code in Xcode 7.2:

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
print("\(convertedNumber)")

It complied, ran within an app of mine, and printed the Int value 123.

Perhaps the error is being thrown from another area of code in your Xcode app, other then the code you think is throwing the error....

Perhaps you are not referencing the version of Swift you think you are...

Note, you should use if let with any conversion attempt:

if let convertedNumber = Int(possibleNumber) {
}

The if let should be used for a conversion no matter how remote the possibility of failure.

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

6 Comments

Thank you for your clarification, I used "if let..." and the same error persists
I updated my answer, I ran you code in Xcode 7.2 without a problem.
You figure out your issue?
Guess what! You're right. I'm practicing on a playground and for some reason I had (var Int = 55) somewhere in the code, lol, and that prevented Int() method to be compiled. Thank you for your help
Awesome! Xcode is a nice IDE but it doesn't have the best exception throwing messages (as far as descriptive about the actual error) and also going/jumping to the actual line of offending code actually throwing the error. Its no Visual Studio IDE but it is still sweet and of course Xcode will get better.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.