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

时间:2020-12-19 22:28:13

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:

Heres是我的代码:

INIT METHOD

INIT方法

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

错误显示在行中:

for character in objectAttributeValues[countOfRun] {

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

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

Thank you a lot!

万分感谢!

2 个解决方案

#1


2  

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

从Swift 2开始,你不能简单地遍历一个String来枚举字符,现在你必须在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]似乎返回一个String,那将是:

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

This is because String does not conform to SequenceType anymore in Swift 2.

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

#2


-3  

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

就像埃里克所说,从Swift 2开始,你就不能再像这样经历一个字符串了。

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 " "
}

#1


2  

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

从Swift 2开始,你不能简单地遍历一个String来枚举字符,现在你必须在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]似乎返回一个String,那将是:

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

This is because String does not conform to SequenceType anymore in Swift 2.

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

#2


-3  

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

就像埃里克所说,从Swift 2开始,你就不能再像这样经历一个字符串了。

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 " "
}