Cannot invoke initializer for type 'Range<String.Index>' with an argument list of type '(start: String.Index, end: String.Index)'

泪湿孤枕 提交于 2019-12-23 17:14:41

问题


let greenHex = hex.substring(with: Range<String.Index>(start: hex.index(hex.startIndex, offsetBy: 2), end: hex.index(hex.startIndex, offsetBy: 4)))

This is Swift3.0, hex is a string, but this code throws an error saying that:

Cannot invoke initializer for type 'Range' with an argument list of type '(start: String.Index, end: String.Index)'


回答1:


Range.init(start:end:) constructor was removed in Swift 3.0 so you initialize a range like follows:

let range = hex.index(hex.startIndex, offsetBy: 2)..<hex.index(hex.startIndex, offsetBy: 4)

which returns a half-open range of type <String.Index>. Then, you can do the following with that:

hex.substring(with: range)


来源:https://stackoverflow.com/questions/39802773/cannot-invoke-initializer-for-type-rangestring-index-with-an-argument-list-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!