【问题标题】:string is not convertible to int (Swift)string 不能转换为 int (Swift)
【发布时间】:2014-10-09 16:33:59
【问题描述】:

这是我的代码(我的实际代码更复杂,但我简化了它,我仍然得到这个错误):

let x = "1" as Int

Xcode 说:

'String' is not convertible to 'Int'

是我遗漏了什么还是这是一个 beta 错误?

【问题讨论】:

    标签: xcode string swift int


    【解决方案1】:

    您不能简单地使用as 进行投射。有一种方法可以显式生成Int

    let x = "1".toInt()
    

    请注意,结果是Int? 类型,因为 Swift 无法预先知道转换是否成功。

    println(x!)
    

    或者:

    let x = "1".toInt()!
    

    【讨论】:

      【解决方案2】:

      希望对你有所帮助

      let x = "1".toInt()
      

      【讨论】:

        猜你喜欢
        • 2014-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多