我有以下代码,但是当我尝试使用以下链接时,x.initial(2).flatten()
我得到一个编译错误说Could not find member flatten
我究竟做错了什么。为了确保我可以继续链接,我在每个链接方法(例如 flatten、initial 等)之后返回 self。
struct $ {
var resultArray: AnyObject[] = []
init(array: AnyObject[]) {
self.resultArray = array
}
mutating func first() -> AnyObject? {
return $.first(self.resultArray)
}
mutating func flatten() -> $ {
self.resultArray = $.flatten(self.resultArray)
return self
}
static func first(array: AnyObject[]) -> AnyObject? {
if array.isEmpty {
return nil
} else {
return array[0]
}
}
mutating func initial() -> $ {
return self.initial(1)
}
mutating func initial(numElements: Int) -> $ {
self.resultArray = $.initial(self.resultArray, numElements: numElements)
return self
}
func value() -> AnyObject[] {
return self.resultArray
}
static func flatten(array: AnyObject[]) -> AnyObject[] {
var resultArr: AnyObject[] = []
for elem : AnyObject in array {
if let val = elem as? AnyObject[] {
resultArr += self.flatten(val)
} else {
resultArr += elem
}
}
return resultArr
}
static func initial(array: AnyObject[]) -> AnyObject[] {
return self.initial(array, numElements: 1)
}
static func initial(array: AnyObject[], numElements: Int) -> AnyObject[] {
var result: AnyObject[] = []
for (index, _) in enumerate((0..array.count - numElements)) {
result += array[index]
}
return result
}
}
var x = $(array: [[1, 2], 3, [[4]]])
var y = x.initial(2).flatten() //Throws the error