Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27153

Swift how to iterate symbols from string

I am trying to get array of strings form string. It's separate symbols that I want to iterate.

 let chars = binaryString.characters.map { String($0) }

    for (index, item) in chars {

      let activeDay = (index, item)

      switch activeDay {
      case (Days.Monday.rawValue, "1"):
        mondayLabel.textColor = UIColor.blackColor()
      case (Days.Monday.rawValue, "0"):
        mondayLabel.textColor = UIColor.grayColor()

But Xcode says Expression type '[String]' is ambiguous without more context

Upvotes: 0

Views: 100

Answers (2)

maxkonovalov
maxkonovalov

Reputation: 3719

For your code to work, you need to add enumerate() to the chars array:

for (index, item) in chars.enumerate() {
    ...

The resulting chars array is of [String] type, and trying to enumerate it as an index-object tuple produces an error.

Upvotes: 1

Oleg Gordiichuk
Oleg Gordiichuk

Reputation: 15512

Try to use all benefits of Swift:

var binaryString = "12312312312312312"

let characters = Array(binaryString.characters)

//Values
for char in characters {
    print(char)
}

//Keys and values
for (index, item) in characters.enumerate() {
    print(index)
    print(item)
}

Upvotes: 1

Related Questions