swift 3

时间:2017-02-10 06:20:49

标签: ios iphone swift

我在Swift 3中得到了这个substringFromIndex的错误。下面是Objective-C中的代码,当我试图在Swift 3中转换时,substringFromIndex显示错误。

    NSString *checkhypenString = [securityNumberField.text substringFromIndex: [securityNumberField.text length] - 5];
    checkhypenString = [checkhypenString substringToIndex:1];

2 个答案:

答案 0 :(得分:0)

试试这个:

var index1 = string1.index(securityNumberField.text.endIndex, offsetBy: -4)

var substring1 = string1.substring(from: index1)

答案 1 :(得分:0)

试试这个 -

 let s = securityNumberField.text! // your string.
    let start = s.startIndex
    let end = s.index(s.endIndex, offsetBy: -5)
    let substring = s[start..<end]
    print(substring)