Arrays 从水果项目数组中的选定项目创建数组

Arrays 从水果项目数组中的选定项目创建数组,arrays,swift,Arrays,Swift,我有一个包含项目的数组和一个包含要从第一个数组中删除的索引的数组: 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"] 如果我的数组如下所示:

我有一个包含项目的数组和一个包含要从第一个数组中删除的索引的数组:

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]
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]

indicesToDelete.sort(>).forEach { arrayFruit.removeAtIndex($0) }
arrayFruit // [{colour "red", type "Apple"}, {colour "green", type "Pear"}]
如果我只是使用上面的代码,我会得到一个错误

let reducedArray = indicesToDelete.reverse().map { arrayFruit.removeAtIndex($0) }//////    error here

我的问题是,水果数组是由对象组成的,我不知道如何调整上面一行中的代码。

缩小的数组不是
map
的结果,而是原始数组,即
arrayFruit
。我建议不要使用
map
,而是使用
forEach
,并这样写:

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]
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]

indicesToDelete.sort(>).forEach { arrayFruit.removeAtIndex($0) }
arrayFruit // [{colour "red", type "Apple"}, {colour "green", type "Pear"}]

对不起,马特,我说得不够具体。在水果数组中,我只想删除项目1(苹果)好吧,你是对的,它很有效,我更喜欢forEach,因为我比map更能理解它