[英]No '..<' candidates produce the expected contextual result type 'NSRange Swift 3
I've migrated to Swift 3.0 and I'm now getting an error on this line: 我已经迁移到Swift 3.0,现在在这条线上出现错误:
let lastFourDigits = (accountNumber as NSString).substringWithRange(accountNumber.endIndex.advancedBy(-4)..<accountNumber.endIndex)
No '..<' candidates produce the expected contextual result type 'NSRange' (aka '_NSRange')
. No '..<' candidates produce the expected contextual result type 'NSRange' (aka '_NSRange')
。 What am I doing wrong here? 我在这里做错了什么?
The syntax for index operations changed (see Get nth character of a string in Swift programming language and the full rationale: Swift Evolution 0065 ): 索引操作的语法已更改(请参阅Swift编程语言中的获取字符串的n个字符以及完整的基本原理: Swift Evolution 0065 ):
let accountNumber = "XX0000000000000000001234"
let lastFourDigits = accountNumber[accountNumber.index(accountNumber.endIndex, offsetBy: -4)..<accountNumber.endIndex]
print("Last 4: \(lastFourDigits)")
No need to use NSString
or NSRange
in Swift. 在Swift中无需使用
NSString
或NSRange
。
A simpler syntax would also be: 一个更简单的语法是:
let lastFourDigits = accountNumber.substring(from: accountNumber.index(accountNumber.endIndex, offsetBy: -4))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.