i'm trying to create an extension for the Array struct in order to add methods just if the contained objects conforms to a particular protocol, but i'm having strange behaviors when i try to access to a method in the extension from a class.
here is my code for a playground
protocol SomeInt {
var theInt: Int {get set}
}
extension Array where Element: SomeInt {
func indexOf(object:SomeInt) -> Index? {
return indexOf({ (obj) -> Bool in
return obj.theInt == object.theInt
})
}
}
class PRR: SomeInt {
var theInt: Int = 0
init(withInt value: Int){
theInt = value
}
}
class container {
var items: [SomeInt]!
}
let obj1 = PRR(withInt: 1)
let obj2 = PRR(withInt: 2)
let arr = [obj1, obj2]
arr.indexOf(obj1) //this succeds
let cont = container()
cont.items = [obj1, obj2]
cont.items.indexOf(obj1) //this doesn't
Any idea on what's wrong??