Array extension to remove object by value

前端 未结 15 1290
我在风中等你
我在风中等你 2020-11-22 08:30
extension Array {
    func removeObject(object: T) {
        var index = find(self, object)
        self.removeAtIndex(index)
    }
}
         


        
15条回答
  •  再見小時候
    2020-11-22 08:47

    I was able to get it working with:

    extension Array {
        mutating func removeObject(object: T) {
            var index: Int?
            for (idx, objectToCompare) in enumerate(self) {
                let to = objectToCompare as T
                if object == to {
                    index = idx
                }
            }
    
            if(index) {
                self.removeAtIndex(index!)
            }
        }
    }
    

提交回复
热议问题