Swift 3.0 iterate over String.Index range

2019-01-25 02:43发布

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])
}

标签: swift swift3
9条回答
太酷不给撩
2楼-- · 2019-01-25 03:08

The best way to do this is :-

 let name = "nick" // The String which we want to print.

  for i in 0..<name.count 
{
   // Operation name[i] is not allowed in Swift, an alternative is
   let index = name.index[name.startIndex, offsetBy: i]
   print(name[index])
}

for more details visit here

查看更多
戒情不戒烟
3楼-- · 2019-01-25 03:14

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
查看更多
爷的心禁止访问
4楼-- · 2019-01-25 03:14

Swift 4.2

Simply:

let m = "alpha"
for i in m.indices {
   print(m[i])
}
查看更多
\"骚年 ilove
5楼-- · 2019-01-25 03:16

Iterating over characters in a string is cleaner in Swift 4:

let myString = "Hello World"    
for char in myString {
    print(char)
}
查看更多
看我几分像从前
6楼-- · 2019-01-25 03:19

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

查看更多
唯我独甜
7楼-- · 2019-01-25 03:20

To concretely demonstrate how to traverse through a range in a string in Swift 4, we can use the where filter in a for loop to filter its execution to the specified range:

func iterateStringByRange(_ sentence: String, from: Int, to: Int) {

    let startIndex = sentence.index(sentence.startIndex, offsetBy: from)
    let endIndex = sentence.index(sentence.startIndex, offsetBy: to)

    for position in sentence.indices where (position >= startIndex && position < endIndex) {
        let char = sentence[position]
        print(char)
    }

}

iterateStringByRange("string", from: 1, to: 3) will print t, r and i

查看更多
登录 后发表回答