Cannot index a value of type '[String]' with an index of type 'Int'

I recently tried to create a custom UITableViewCell class by adding a slight improved UITextField. I also code in swift 2, and I realized this error by recompiling the project in beta version of Xcode 7. I initialized the array by calling the custom init method.

Heres is my code:

INITIATION METHOD

init(dataObject: [NSManagedObject]!, objectAttributeValues: [String]!,placeholder: String!, segmentedControl: UISegmentedControl?) {
    self.dataObject = dataObject
    self.Placeholder.text = placeholder
    self.objectAttributeValues = objectAttributeValues

    if segmentedControl != nil {
        self.segmentedControl = segmentedControl!
        didHaveSegmentedControl = true
    }
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

IBACTION EditingChanged

currentInputCount = "\(TextField.text)".characters.count
    var indexOfArray: Int = 0
    countOfRun = 0


    if currentInputCount == 0 {
        countOfRun = 0
        formerInputCount = 0
        editingDidEndForTextField = false
        concatenedWord = []
        Placeholder.text = ""
    }

    if !editingDidEndForTextField && currentInputCount > 0 {
        while countOfRun < dataObject.count {
            if !backspaceWasPressed() {
                var arrayOfCharacters: [String] = []
                if countOfRun <= dataObject.count - 1 {
                    for character in objectAttributeValues[countOfRun] {
                        let string = String(character)
                        arrayOfCharacters.append(string)
                    }
                }
                var convertedStringInFormOfArrayOfStrings: [String] = arrayOfCharacters
                if currentInputCount == 1 {
                    concatenedWord.append(convertedStringInFormOfArrayOfStrings[currentInputCount-1])
                }
                else if countOfRun > 0 {
                    if objectAttributeValues[countOfRun].characters.count != concatenedWord[countOfRun].characters.count {
                        concatenedWord[countOfRun] = concatenedWord[countOfRun] + convertedStringInFormOfArrayOfStrings[currentInputCount-1]
                    }
                }
                countOfRun += 1
            }

The error is displayed on the line:

for character in objectAttributeValues[countOfRun] {

I have no idea what it could be ... Someone can help me.

Thank you very much!

+4
source share
2 answers

As in Swift 2, you can’t just iterate over Stringto enumerate more characters, now you have to call the method charactersfor the string itself:

for letter in "hello".characters {
    print(letter)
}

, , , objectAttributeValues[countOfRun], , , :

for character in objectAttributeValues[countOfRun].characters {
    ...
}

, String SequenceType Swift 2.

+2

, Swift 2 String.

-, , , .

func example(s: Int, str: String) -> Character {
   var count = 0
   for character in str {
       if(count == s)
          return character
       count++
   }
  return " "
}
-3

All Articles