Removing object from array in Swift 3

前端 未结 13 2075
余生分开走
余生分开走 2020-12-08 12:49

In my application I added one object in array when select cell and unselect and remove object when re-select cell. I used that code but give me error.

extens         


        
13条回答
  •  时光取名叫无心
    2020-12-08 13:02

    Extension for array to do it easily and allow chaining for Swift 4.2 and up:

    public extension Array where Element: Equatable {
        @discardableResult
        public mutating func remove(_ item: Element) -> Array {
            if let index = firstIndex(where: { item == $0 }) {
                remove(at: index)
            }
            return self
        }
    
        @discardableResult
        public mutating func removeAll(_ item: Element) -> Array {
            removeAll(where: { item == $0 })
            return self
        }
    }
    

提交回复
热议问题