简体   繁体   English

没有'.. <'候选者产生预期的上下文结果类型'NSRange Swift 3

[英]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中无需使用NSStringNSRange

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.

相关问题 Swift 3-没有“排序”候选人产生预期的上下文结果类型“ NSMutableArray” - Swift 3 - No 'sort' candidates produce the expected contextual result type 'NSMutableArray' 没有&#39;|&#39; 候选人产生预期的上下文结果类型&#39;NSTextStorageEditActions&#39; - No '|' candidates produce the expected contextual result type 'NSTextStorageEditActions' 收到错误没有&#39;+&#39;候选者产生预期的上下文结果类型&#39;NSString&#39; - Getting a error No '+' candidates produce the expected contextual result type 'NSString' 没有&#39;decodeIfPresent&#39;候选者产生预期的上下文结果类型&#39;[ModelMemberSubCategory]!” - No 'decodeIfPresent' candidates produce the expected contextual result type '[ModelMemberSubCategory]!' No * candidate产生预期的结果类型FloatingPointRoundingRule - No * candidates produce the expected result type FloatingPointRoundingRule URLSession 产生“()”,而不是预期的上下文结果类型 - URLSession produces '()', not the expected contextual result type append&#39; 产生 &#39;()&#39;,而不是预期的上下文结果类型 &#39;String - append' produces '()', not the expected contextual result type 'String 使用Swift从NSString查找String作为NSRange的出现,结果NSRange将在NSAttributedString中使用 - Finding occurrences of String as NSRange from an NSString using Swift, result NSRange to be used in NSAttributedString 无法将类型NSRange的值转换为预期的参数类型 - Cannot convert value of type NSRange to expected argument type Array.map()产生&#39;[T]&#39;,而不是预期的上下文结果类型&#39;&#39;String:Any?]&#39; - Array.map() produces '[T]', not the expected contextual result type '[String: Any?]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM