我创建了一个自定义类MyArray
用于练习和学习。我在该类和一个内部数组中创建了一些方法。在类内部定义的内部数组上使用index(of: )
方法时遇到错误。我班上的代码如下:
public class MyArray<Element> {
private var factorialNumber: Double = 0
private var arr: [Element] = []
internal var instance: MyArray?
private init() {}
public func removeElement(_ item: Element) -> [Element] {
var found = true
while found {
if arr.contains(item) { // ERROR: Missing argument label '"where:" in call
let index = arr.index(of: item) // Error: cannot invoke index with an argument list of type (of: Element)
arr.remove(at: index!)
} else {
found = false
}
}
return arr
}
public func removeFirstOccurance(of value: Element) -> [Element] {
let index : Int = arr.index(of: value)
arr.remove(at: index!) // ERROR: Cannot force wrap value of non optional type Int
return arr
}
}