xcode 无法使用类型为“Int”的索引为“[String]”类型的值添加下标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30875313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:19:12  来源:igfitidea点击:

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

iosxcodeswiftswift2xcode7

提问by Tom Kuschka

I'm recently trying to build a custom UITableViewCell Class by adding a minor improved UITextField. I am also coding in swift 2 and I realised this error by recompiling the project in Xcode 7 beta. I initialised the array by call a custom init method.

我最近试图通过添加一个小的改进的 UITextField 来构建一个自定义的 UITableViewCell 类。我也在 swift 2 中编码,我通过在 Xcode 7 beta 中重新编译项目来意识到这个错误。我通过调用自定义 init 方法初始化了数组。

Heres is my Code:

这是我的代码:

INIT 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

IBACTION 编辑已更改

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 appears in line:

错误出现在一行中:

for character in objectAttributeValues[countOfRun] {

I have no clue what it could be... Can anybody help me.

我不知道它可能是什么......任何人都可以帮助我。

Thank you a lot!

非常感谢!

采纳答案by Eric Aya

As of Swift 2, you can't simply loop over a Stringto enumerate the characters anymore, now you have to call the charactersmethod on the String itself:

从 Swift 2 开始,您不能再简单地循环遍历 aString来枚举字符,现在您必须characters在 String 本身上调用该方法:

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

So for you, given that objectAttributeValues[countOfRun]seems to return a String, that would be:

所以对你来说,考虑到它objectAttributeValues[countOfRun]似乎返回一个字符串,那就是:

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

This is because Stringdoes not conform to SequenceTypeanymore in Swift 2.

这是因为在 Swift 2String中不再符合SequenceType

回答by repo

Like Eric said, as of Swift 2 you can no longer just go through a String like that.

就像 Eric 说的,从 Swift 2 开始,你不能再像那样遍历 String 了。

In my opinion the easiest, and most likely the best programatically correct way to do this would be with a function.

在我看来,最简单,也最有可能是最好的编程正确方法是使用函数。

so

所以

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