Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3.0 iterate over String.Index range

Tags:

swift

swift3

The following was possible with Swift 2.2:

let m = "alpha"
for i in m.startIndex..<m.endIndex {
    print(m[i])
}
a
l
p
h
a

With 3.0, we get the following error:

Type 'Range' (aka 'Range') does not conform to protocol 'Sequence'

I am trying to do a very simple operation with strings in swift -- simply traverse through the first half of the string (or a more generic problem: traverse through a range of a string).

I can do the following:

let s = "string"
var midIndex = s.index(s.startIndex, offsetBy: s.characters.count/2)
let r = Range(s.startIndex..<midIndex)
print(s[r])

But here I'm not really traversing the string. So the question is: how do I traverse through a range of a given string. Like:

for i in Range(s.startIndex..<s.midIndex) {
    print(s[i])
}
like image 866
p0lAris Avatar asked Jul 15 '16 02:07

p0lAris


3 Answers

You can traverse a string by using indices property of the characters property like this:

let letters = "string"
let middle = letters.index(letters.startIndex, offsetBy: letters.characters.count / 2)

for index in letters.characters.indices {

    // to traverse to half the length of string 
    if index == middle { break }  // s, t, r

    print(letters[index])  // s, t, r, i, n, g
}

From the documentation in section Strings and Characters - Counting Characters:

Extended grapheme clusters can be composed of one or more Unicode scalars. This means that different characters—and different representations of the same character—can require different amounts of memory to store. Because of this, characters in Swift do not each take up the same amount of memory within a string’s representation. As a result, the number of characters in a string cannot be calculated without iterating through the string to determine its extended grapheme cluster boundaries.

emphasis is my own.

This will not work:

let secondChar = letters[1] 
// error: subscript is unavailable, cannot subscript String with an Int
like image 83
Khundragpan Avatar answered Oct 18 '22 18:10

Khundragpan


Another option is to use enumerated() e.g:

let string = "Hello World"    
for (index, char) in string.characters.enumerated() {
    print(char)
}

or for Swift 4 just use

let string = "Hello World"    
for (index, char) in string.enumerated() {
    print(char)
}
like image 35
Andrew Avatar answered Oct 18 '22 19:10

Andrew


Use the following:

for i in s.characters.indices[s.startIndex..<s.endIndex] {
  print(s[i])
}

Taken from Migrating to Swift 2.3 or Swift 3 from Swift 2.2

like image 7
Dmitry Avatar answered Oct 18 '22 19:10

Dmitry