-1
我有一個元素的數組與索引數組從第一陣列刪除:在水果項的數組創建所選項目的數組
如果var array = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let indicesToDelete = [4, 8]
let reducedArray = indicesToDelete.reverse().map { array.removeAtIndex($0) }
reducedArray // prints ["i","e"]
我的數組是這樣的:
class Fruit{
let colour: String
let type: String
init(colour:String, type: String){
self.colour = colour
self.type = type
}
}
var arrayFruit = [Fruit(colour: "red", type: "Apple"),Fruit(colour: "green", type: "Pear"), Fruit(colour: "yellow", type: "Banana"),Fruit(colour: "orange", type: "Orange")]
let indicesToDelete = [2,3]
如果我只是使用上面的代碼,我得到一個錯誤。
let reducedArray = indicesToDelete.reverse().map { arrayFruit.removeAtIndex($0) }////// error here
我的問題是fruitArray是由對象組成的,我不知道如何調整上面的代碼。
對不起,馬特,我不夠具體。在fruitArray中,我只想刪除例如第1項(蘋果) – kangarooChris
嗯,你是對的,它的工作原理,我喜歡每個更好,因爲我可以比地圖更好地理解它 – kangarooChris