Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing protocol references

I have an array of protocols. now I want to remove an item from the array, by finding the index of the protocol with the array. however, when comparing the protocol object with the items in the array, the compiler warns with:

'Protocol' does not conform to AnyObject

protocol SomeProtocol {}
var list:[SomeProtocol] = []
func add(some:SomeProtocol) { list+=some }
func remove(some:SomeProtocol) {
    var index = -1
    for i in 0...list.count-1 { if [i] === some { index = i } }
    if index >= 0 { list.removeAtIndex(index) }
}
like image 406
user3906135 Avatar asked Aug 04 '14 10:08

user3906135


1 Answers

If you derive only classes for the protocol, you can change protocol definition to:

protocol SomeProtocol: class {}

Then you will be able to use references with this protocol.

like image 69
Oleg Gordeev Avatar answered Oct 10 '22 07:10

Oleg Gordeev