0

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

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • Possible duplicate of [Iterate through a String Swift 2.0](http://stackoverflow.com/questions/30767594/iterate-through-a-string-swift-2-0) – mixel Sep 13 '16 at 16:07

2 Answers2

1

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)
}
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
1

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.

maxkonovalov
  • 3,651
  • 34
  • 36