In Swift 3 SE-0065 changed the indexing model for Collections, wherein responsibility for index traversal is moved from the index to the collection itself. For example, instead of writing i.successor(), onehas to write c.index(after: i).
What this means in terms of accessing Strings at a certain index, is that instead of writing this:
let aStringName = "Bar Baz"
aStringName[aStringName.startIndex.advancedBy(3)]
...we now have to write this:
aStringName[aStringName.index(aStringName.startIndex, offsetBy: 3)]
This seems incredibly redundant, as aStringName is mentioned thrice. So my question is if there's any way to get around this (aside from writing an extension to String)?
aStringNamecould be referred to byselfand in many applications, hence omitted.Array(s.characters)[3]would be concise, but ineffective for long strings.s.characters.dropFirst(3).firstis an alternative, but not really more nice. – Often you need the characters sequentially, and then you should enumerate them instead of indexing repeatedly.enumerated()method produce a lazy sequence forStrings? Because otherwise it seems quite expensive to enumerate the characters.Stringto add a subscript byInt