我在 Swift Playground 中尝试了以下操作:
class C {
init(test: Integer) {
let simpleList:String[] = ["A","B","C"]
simpleList[test]
println(simpleList[test])
}
}
我得到一个错误:
Could not find an overload for subscript that accepts the supplied arguments
当我尝试为列表编制索引时,它会在多个地方弹出。
最佳答案
这个错误通常有两个原因:
在您的情况下,您需要将 test
更改为 Int
类型,如下所示:(test: Int)
。
引发类似错误的另一种情况是由于 simpleList
是 Any
或 AnyObject
类型。
在这种情况下,您需要在索引之前将其强制转换为类型 String[]
或其他 Swift 数组类型
关于 swift Playground : Indexing a List within a class gives an error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24167887/