Removing object from array in Swift 3

前端 未结 13 2045
余生分开走
余生分开走 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
        }
    }
    
    0 讨论(0)
  • 2020-12-08 13:05

    Try this in Swift 3

    array.remove(at: Index)
    

    Instead of

    array.removeAtIndex(index)
    

    Update

    "Declaration is only valid at file scope".
    

    Make sure the object is in scope. You can give scope "internal", which is default.

    index(of:<Object>) to work, class should conform to Equatable

    0 讨论(0)
  • In Swift 5, Use this Extension:

    extension Array where Element: Equatable{
        mutating func remove (element: Element) {
            if let i = self.firstIndex(of: element) {
                self.remove(at: i)
            }
        }
    }
    

    example:

    var array = ["alpha", "beta", "gamma"]
    array.remove(element: "beta")
    

    In Swift 3, Use this Extension:

    extension Array where Element: Equatable{
        mutating func remove (element: Element) {
            if let i = self.index(of: element) {
                self.remove(at: i)
            }
        }
    }
    

    example:

    var array = ["alpha", "beta", "gamma"]
    array.remove(element: "beta")
    
    0 讨论(0)
  • 2020-12-08 13:07

    For Swift 3, you can use index(where:) and include a closure that does the comparison of an object in the array ($0) with whatever you are looking for.

    var array = ["alpha", "beta", "gamma"]
    if let index = array.index(where: {$0 == "beta"}) {
      array.remove(at: index)
    }
    
    0 讨论(0)
  • 2020-12-08 13:07

    This is what I've used (Swift 5)...

        extension Array where Element:Equatable
        {
            @discardableResult
            mutating func removeFirst(_ item:Any ) -> Any? {
                for index in 0..<self.count {
                    if(item as? Element == self[index]) {
                        return self.remove(at: index)
                    }
                }
                return nil
            }
            @discardableResult
            mutating func removeLast(_ item:Any ) -> Any? {
                var index = self.count-1
                while index >= 0 {
                    if(item as? Element == self[index]) {
                        return self.remove(at: index)
                    }
                    index -= 1
                }
                return nil
            }
        }
    
        var arrContacts:[String] = ["A","B","D","C","B","D"]
        var contacts: [Any] = ["B","D"]
        print(arrContacts)
        var index = 1
        arrContacts.removeFirst(contacts[index])
        print(arrContacts)
        index = 0
        arrContacts.removeLast(contacts[index])
        print(arrContacts)
    

    Results:

       ["A", "B", "D", "C", "B", "D"]
       ["A", "B", "C", "B", "D"]
       ["A", "B", "C", "D"]
    

    Important: The array from which you remove items must contain Equatable elements (such as objects, strings, number, etc.)

    0 讨论(0)
  • 2020-12-08 13:11

    The Swift equivalent to NSMutableArray's removeObject is:

    var array = ["alpha", "beta", "gamma"]
    
    if let index = array.firstIndex(of: "beta") {
        array.remove(at: index)
    }
    

    if the objects are unique. There is no need at all to cast to NSArray and use indexOfObject:

    The API index(of: also works but this causes an unnecessary implicit bridge cast to NSArray.

    If there are multiple occurrences of the same object use filter. However in cases like data source arrays where an index is associated with a particular object firstIndex(of is preferable because it's faster than filter.

    Update:

    In Swift 4.2+ you can remove one or multiple occurrences of beta with removeAll(where:):

    array.removeAll{$0 == "beta"}
    
    0 讨论(0)
提交回复
热议问题