I was working on this tutorial on Swift when I ran into this problem.
code:
var languagesLearned: String = "3"
if let num: Int? = Int(languagesLearned)
{
print("It is a number")
}
else
{
print("It is not a number")
}
error:
Playground execution failed: /var/folders/f7/0j8dbxls0kv0l9d6jwk30f2h0000gn/T/lldb/20598/playground134.swift:72:20: error: cannot invoke 'init' with an argument of type '@lvalue String'
if let num: Int? = Int(languagesLearned)
^~~~~~~~~~~~~~~~~~~~~
I can't find anything on why I'm getting this kind of error. I also have no idea what it's saying about init.
Corrected Code After Answer
var languagesLearned: String = "3"
var languagesLearnedNum = languagesLearned.toInt()
if let num = languagesLearnedNum
{
print("It is a number")
}
else
{
print("It is not a number")
}
在 Swift 1.x 中,使用:
languagesLearned.toInt()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.