[英]Playground execution failed: <EXPR>:15:33: error: type 'Int' does not conform to protocol 'BooleanType'
[英]Playground execution failed on Int() type conversion
當我遇到這個問題時,我正在編寫這個關於 Swift 的教程。
代碼:
var languagesLearned: String = "3"
if let num: Int? = Int(languagesLearned)
{
print("It is a number")
}
else
{
print("It is not a number")
}
錯誤:
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)
^~~~~~~~~~~~~~~~~~~~~
我找不到任何關於為什么我會收到這種錯誤的信息。 我也不知道它在說什么關於 init。
回答后更正的代碼
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.