Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot invoke initializer for type 'Range<_>' with an argument list of type '(Range<String.Index>)' in func substring(start:end:) #85

Open
boywithaxe opened this issue Jun 5, 2018 · 8 comments

Comments

Projects
None yet
5 participants
@boywithaxe
Copy link

commented Jun 5, 2018


func substring(_ start: Int, end: Int) -> String {
return String(self[Range(self.index(self.startIndex, offsetBy: start) ..< self.index(self.startIndex, offsetBy: end))])

    }

the above function contains a method resulting in the following error:

Cannot invoke initializer for type 'Range<_>' with an argument list of type '(Range<String.Index>)' in Xcode 10 beta 1.

I appreciate this is a very early report, but does seem to be a swift version issue.

@LeonardoCardoso

This comment has been minimized.

Copy link
Owner

commented Jun 6, 2018

Most probably it is. I'll have a look ASAP.

@LeonardoCardoso

This comment has been minimized.

Copy link
Owner

commented Jun 7, 2018

Changes in master.

@MichaelLuoSyd

This comment has been minimized.

Copy link

commented Jul 3, 2018

Hi, may I know how to solve this issue please? I am having the same problem. Thanks

@LeonardoCardoso

This comment has been minimized.

Copy link
Owner

commented Jul 4, 2018

@MichaelLuoSyd Changes are in master!

@jpmhglance

This comment has been minimized.

Copy link

commented Sep 13, 2018

Was this it?

-         return String(self[Range(self.index(self.startIndex, offsetBy: start) ..< self.index(self.startIndex, offsetBy: end))])
+         return self.substring(NSRange(location: start, length: end - start))

I fixed a similar error in my code this way instead:

-                        self.text = String(str[Range(startIndex ..< nextToEndIndex)])
+                        self.text = String(str[startIndex ..< nextToEndIndex])
@asharmaVelsof

This comment has been minimized.

Copy link

commented Oct 16, 2018

I fixed the error by replacing the code with

let range = self.index(self.startIndex, offsetBy: r.lowerBound)..<self.index(self.startIndex, offsetBy: r.upperBound) return String(self[range])

@boywithaxe

This comment has been minimized.

Copy link
Author

commented Oct 26, 2018

That's the line that fixes it. the issue recurred in the latest version, but making the following change fixes it in Swift 4 and up

`

  •     return String(self[Range(self.index(self.startIndex, offsetBy: start) ..< self.index(self.startIndex, offsetBy: end))])
    
  •     return self.substring(NSRange(location: start, length: end - start))`
    
@jpmhglance

This comment has been minimized.

Copy link

commented Oct 26, 2018

@boywithaxe beware though, substring(with: NSRange) is an NSString method which indexes strings differently that Swift strings. Your fix will definitely have different behaviour if the string contains certain emoji character, look into Unicode Extended Grapheme Clusters.

Did you try just taking out Range( .. )? A la:

return String(self[self.index(self.startIndex, offsetBy: start) ..< self.index(self.startIndex, offsetBy: end)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.