gpt4 book ai didi

ios - Int() 类型转换时 Playground 执行失败

转载 作者:行者123 更新时间:2023-11-28 06:58:19 26 4
gpt4 key购买 nike

当我遇到这个问题时,我正在使用 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()

关于ios - Int() 类型转换时 Playground 执行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32731491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com