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
[SR-10456] String index(_:offsetBy:limitedBy:) don't return nil as expected #52856
Comments
The |
Comment by Fon (JIRA) It's really a documentation problem? Please see the code below. let j = s.index(s.startIndex, offsetBy: 6, limitedBy: s.endIndex) |
No, |
Comment by Fon (JIRA) let s = "Swift"
let startIndex=s.startIndex
var endIndex=s.endIndex
//These are valid.
if var i=s.index(s.startIndex, offsetBy: 5, limitedBy: s.endIndex){
print(s.formIndex(&i, offsetBy: 0, limitedBy:s.endIndex))//prints true
}
print(s.formIndex(&endIndex, offsetBy: 0, limitedBy: s.endIndex))//prints true
print(s[startIndex])//prints "S"
print(s[..<endIndex])//prints "Swift"
print(s[s.endIndex])//Crashed error String index is out of bounds.
let name = "Marie Curie"
let firstSpace = name.firstIndex(of: " ") ?? name.endIndex
let firstName = name[..<firstSpace]
// firstName == "Marie"
I got it. It's a designed operation. I can use i only by s[..<endIndex]. But why don't allow to subscript a string by endIndex. |
|
Comment by Alexander Ignition (JIRA) @belkadan can we update the documentation so that it is not misleading ? for example let numbers = [10, 20, 30, 40, 50]
if let i = numbers.index(numbers.startIndex,
offsetBy: 4,
limitedBy: numbers.index(before: numbers.endIndex)) {
print(numbers[i])
} |
I'm not at Apple anymore, so I'm not sure who's handling documentation these days. @krilnon? |
That’d be amartini (JIRA User). |
Attachment: Download
Environment
Xcode 10.2,Swift 5
Additional Detail from JIRA
md5: 316746d417790f24988e58e3a206990a
Issue Description:
let s = "Swift"
}
The text was updated successfully, but these errors were encountered: