Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove an element from an array of protocols in Swift?

Swift's protocol implementation is really driving me crazy right now. I have an array observers defined over a custom protocol Observing and I am trying to remove an element from that array given the element, but Swift is complaining that Observing doesn't implement Identifiable (which I think is actually another of my own protocols, unless there's also a system protocol called that). All I want to do is a reference comparison and remove the actual object. I don't care about performing any kind of comparison on the objects.

var observers = [Observing]()

func removeObserver( observer: Observing ) {
    for i in 0 ..< self.observers.count {
        if self.observers[i] == observer { // <='Observing' is not convertible to 'Identifiable'...?
            self.observers.removeAtIndex(i)
            break
        }
    }
}
like image 313
devios1 Avatar asked Jul 26 '14 19:07

devios1


People also ask

How do you remove an element from an array array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

How do I remove an element from an array in Swift 5?

To remove an element from the Swift Array, use array. remove(at:index) method. Remember that the index of an array starts at 0. If you want to remove ith element use the index as (i-1).

How do you remove an object from an array?

To remove an object from an array by its value:Call the findIndex() method to get the index of the object in the array. Use the splice() method to remove the element at that index. The splice method changes the contents of the array by removing or replacing existing elements.

Which of these methods are used to remove an element from an array?

There are different methods and techniques you can use to remove elements from JavaScript arrays: pop - Removes from the End of an Array. shift - Removes from the beginning of an Array. splice - removes from a specific Array index.


1 Answers

The == operator checks for value-equality in Swift, and for there is no default implementation for it. What you want is reference equality, which you can get using === and !== in Swift.

See the documentation for more details.

Identity Operators

Because classes are reference types, it is possible for multiple constants and variables to refer to the same single instance of a class behind the scenes. (The same is not true for structures and enumerations, because they are always copied when they are assigned to a constant or variable, or passed to a function.)

It can sometimes be useful to find out if two constants or variables refer to exactly the same instance of a class. To enable this, Swift provides two identity operators:

Identical to (===) Not identical to (!==)

Note that for the === operator to work, the objects must conform to the AnyObject protocol. You can guarantee this by suffixing the protocol with ": class", like this:

protocol SomeProtocol : class { ... }
like image 75
ahruss Avatar answered Sep 23 '22 06:09

ahruss