【问题标题】:Swift 2 toInt() not working [duplicate]Swift 2 toInt() 不工作[重复]
【发布时间】:2016-01-28 16:29:05
【问题描述】:

我刚刚升级到 Swift 2.0,不确定为什么这行代码不起作用。我知道 toInt() 已在 Swift 2.0 中删除,但我不知道如何更新这行代码,所以它会在 Swift 2.0 中工作。有什么建议吗?

旧行:

followers.setTitle((followers.currentTitle!.toInt()! - 1).description, forState: UIControlState.Normal)

我试过这条线没有成功:

followers.setTitle((followers.currentTitle!.Int()! + 1).description, forState: UIControlState.Normal)

【问题讨论】:

  • 声明一个Int 变量让追随者计数器进行数学运算并使用String(counter) 更新标题可能更有效。

标签: ios swift


【解决方案1】:

阅读错误信息

toInt() 不可用:使用 Int() 初始化器

意思

Int(followers.currentTitle!)!

【讨论】:

  • 不要鼓励他们含蓄地拆开东西。在你知道之前,他们会发布另一个关于它的问题:p
  • 这与问题无关。 currentTitle 可以安全地进行整数转换。
【解决方案2】:

试试这个:

followers.setTitle((Int(followers.currentTitle!)! + 1).description, forState: UIControlState.Normal)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-24
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多