3

I was hoping to do something like this:

In netServiceBrowser:didFindService:moreComing:

[self.foundServices addObject:aNetService];

And in netServiceBrowser:didRemoveService:moreComing:

[self.foundServices removeObject:aNetService];

However, the services returned aren't retained by the NetServiceBrowser, and so the service given in didRemoveService isn't the same object as those in the array. How do I compare the services to ensure that the one I remove is the correct one?

Zeophlite
  • 1,607
  • 3
  • 19
  • 36

2 Answers2

5

You're over-thinking this, and creating a problem in your head that doesn't exist. Use removeObject: on the object passed into didRemoveService:. removeObject: removes based on the object's response to isEqual:, not the address or identity of the object. So this will just work.

The answer really is as simple as:

[self.foundServices removeObject:aNetService];

Apple's description of removeObject: explains this:

This method uses indexOfObject: to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined on the basis of an object’s response to the isEqual: message.

(For completeness, Apple does offer a function that removes an object by address. That's removeObjectIdenticalTo:. That's not the behaviour you want here, however. Just use removeObject:.)

References:

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
  • This question was asked nearly 2 years ago, and I don't have access to the original code. The problem described above DID happen, I'm guessing it was due to perhaps pre-release code, or something funky happening in code. My answer did work in the end, but I'm assuming that my specific problem doesn't occur anymore. – Zeophlite Oct 28 '13 at 04:31
  • This answer was also almost two years ago, and I'd been developing for iOS since 2010. Including, yes, NSNetService work. I don't doubt what you thought you saw, but you were incorrect in one of your assumptions. This has always worked. In fact, you can see I posted this answer **on the same day** as you posted yours. – Steven Fisher Oct 28 '13 at 18:17
  • And to be completely clear: I completely sympathize, because at the time I thought `removeObject:` wouldn't do what I wanted. Then I tested it and found it did *exactly* what I needed it to do. – Steven Fisher Oct 28 '13 at 18:56
0

Short answer, I ended up having to use this, but I can't reproduce the problem anymore. Use the selected answer.

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    [self.foundServices addObject:aNetService];
}


- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    NSNetService *found = nil;

    for(NSNetService *ns in self.foundServices) {
        if([ns isEqualTo:aNetService]) {
            found = ns;
        }
    }

    if(found) {
        [self.foundServices removeObject:found];
    }
}
Zeophlite
  • 1,607
  • 3
  • 19
  • 36