swift - 在 Swift 3 中解包可选整数的正确方法

标签 swift swift3

在这里设置fullLength 的正确方法是什么?

var index = foo.index(foo.startIndex, offsetBy: SOME_NUM)
let length = Int(foo.substring(to: index))

if let fullLength = length? + SOME_NUM {
  return
}

这里的问题是 length? 是可选的。我有这个工作:

fullLength = 长度! + SOME_NUM,但我不想冒险在 length 为 nil 的情况下使用 !

最佳答案

你可以改写为

if let length = length {
    let fullLength = length + SOME_NUM
}

关于swift - 在 Swift 3 中解包可选整数的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41453807/

相关文章:

ios - [错误] : Type info TC, N,D 尚不支持

objective-c - 在 Swift 中获取 Objective-C 接口(interface)?

ios - CLLocation 为不同的 iPhone 设备返回不同的 CLLocationSpeed 值

ios - 为什么prepare for segue不能快速发送图像?

ios - 如何在Swift 3中禁用选项卡栏的用户交互?

ios - 如何导航回 tvOS?

ios - 通过动画和中心更改宽度 UIView

ios - "continue userActivity"方法未调用,尽管 Firebase 深层链接已成功打开应用程序

swift - 为什么让 x 作为 Float 大小写在 Any 变量的开关中不匹配?

swift - 我们如何使键盘快速出现在textView下方?