Array extension to remove object by value

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


        
相关标签:
15条回答
  • 2020-11-22 08:36

    I managed to remove a [String:AnyObject] from an array [[String:AnyObject]] by implementing a count outside of a for loop to represent the index since .find and .filter are not compatible with [String:AnyObject].

    let additionValue = productHarvestChoices[trueIndex]["name"] as! String
    var count = 0
    for productHarvestChoice in productHarvestChoices {
      if productHarvestChoice["name"] as! String == additionValue {
        productHarvestChoices.removeAtIndex(count)
      }
      count = count + 1
    }
    
    0 讨论(0)
  • 2020-11-22 08:39

    No need to extend:

    var ra = [7, 2, 5, 5, 4, 5, 3, 4, 2]
    
    print(ra)                           // [7, 2, 5, 5, 4, 5, 3, 4, 2]
    
    ra.removeAll(where: { $0 == 5 })
    
    print(ra)                           // [7, 2, 4, 3, 4, 2]
    
    if let i = ra.firstIndex(of: 4) {
        ra.remove(at: i)
    }
    
    print(ra)                           // [7, 2, 3, 4, 2]
    
    if let j = ra.lastIndex(of: 2) {
        ra.remove(at: j)
    }
    
    print(ra)                           // [7, 2, 3, 4]
    
    0 讨论(0)
  • 2020-11-22 08:40

    Using indexOf instead of a for or enumerate:

    extension Array where Element: Equatable {
    
       mutating func removeElement(element: Element) -> Element? {
          if let index = indexOf(element) {
             return removeAtIndex(index)
          }
          return nil
       }
    
       mutating func removeAllOccurrencesOfElement(element: Element) -> Int {
           var occurrences = 0
           while true {
              if let index = indexOf(element) {
                 removeAtIndex(index)
                 occurrences++
              } else {
                 return occurrences
              }
           }
       }   
    }
    
    0 讨论(0)
  • 2020-11-22 08:41

    what about to use filtering? the following works quite well even with [AnyObject].

    import Foundation
    extension Array {
        mutating func removeObject<T where T : Equatable>(obj: T) {
            self = self.filter({$0 as? T != obj})
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 08:44

    Maybe I didn't understand the question.

    Why wouldn't this work?

    import Foundation
    extension Array where Element: Equatable {
        mutating func removeObject(object: Element) {
            if let index = self.firstIndex(of: object) {
                self.remove(at: index)
            }
        }
    }
    
    var testArray = [1,2,3,4,5,6,7,8,9,0]
    testArray.removeObject(object: 6)
    let newArray = testArray
    
    var testArray2 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
    testArray2.removeObject(object: "6")
    let newArray2 = testArray2
    
    0 讨论(0)
  • 2020-11-22 08:45

    I finally ended up with following code.

    extension Array where Element: Equatable {
    
        mutating func remove<Element: Equatable>(item: Element) -> Array {
            self = self.filter { $0 as? Element != item }
            return self
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题