swift - Startindex swift 3 问题

标签 swift swift3

<分区>

我收到 startIndex 错误。错误是“‘string.index’类型的值(又名‘string.characterview.index’)没有成员‘advanced’”。这是代码和错误图像。

public override final var secureTextEntry: Bool {
    didSet {
        let textFieldArray: [UITextField?] = [numberInputTextField, cvcTextField, monthTextField, yearTextField]
        textFieldArray.forEach({$0?.isSecureTextEntry = secureTextEntry})
    }
}

Image of Error of isfirstresponder

最佳答案

在 swift 3 中,你不能再使用 advanced、successor 或 predecessor,而是需要使用

let indexAfter = someString.index(after: someIndex)
let indexBefore = someString.index(before: someIndex)
let anyOtherIndex = someString.index(someIndex, offsetBy: distance)

所以你的代码应该是这样的

let index = (hasOverflow) ? 
text.index(text.startIndex, offsetBy: expectedInputLength) :
text.index(text.startIndex, offsetBy: text.characters.count)

作为旁注,

text.index(text.startIndex, offsetBy: text.characters.count)

其实和

是一样的
text.endIndex

所以你可以用这个代替

let index = (hasOverflow) ? 
text.index(text.startIndex, offsetBy: expectedInputLength) :
text.endIndex

关于swift - Startindex swift 3 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40195955/

相关文章:

swift3 - 如何在不造成 fps 延迟的情况下移动 Sprite

swift - 定义指数运算符

ios - 在数组上使用 NSUserDefaults

ios - 如何在 Swift 中手动启用/禁用键盘返回键?

swift - 在我的 iPhone 上没有收到推送通知,但 Firebase Functions 说它已成功发送?

ios - 无法覆盖在另一个模块 swift 的类扩展中声明的打开方法

swift - Swift3 中不明确的运算符分辨率回归?

ios - 在 XCUITest 中读取本地 JSON 文件

iOS:自动布局的问题。模拟器结果随着 Storyboard的变化而变化

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