Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

swift3

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)'

like image 476
Chacha Avatar asked Dec 22 '25 16:12

Chacha


1 Answers

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)
like image 139
Ozgur Vatansever Avatar answered Dec 24 '25 09:12

Ozgur Vatansever