Reputation: 307
Step 1:
I declared a protocol, named ARDevice
:
protocol ARDevice {
var deviceName:String{get}
}
Step 2:
Then I used it to extend NSNetService
:
extension NSNetService:ARDevice{
var deviceName:String{
get{
return self.name
}
}
}
Step 3: I created an array:
var deviceList = [ARDevice]()
Step 4: I want to use contains(:)
in a call back method, but I can't:
How can I do that? Am I have to implement any protocol?
Upvotes: 3
Views: 1348
Reputation: 539775
if !deviceList.contains(service) { }
can only be used if the elements of the deviceList
array – in your
case ARDevice
– conform to the Equatable
protocol. In particular,
a ==
operator must be defined for them.
The easiest solution here is to use the "predicate-based" contains()
method:
if !deviceList.contains ({ $0.deviceName == service.deviceName }) {
deviceList.append(service)
}
Upvotes: 5