NetServiceBrowser 删除后会提供不同的 NetService
我希望做这样的事情:
在 netServiceBrowser:didFindService:moreComing:
[self.foundServices addObject:aNetService];
和 netServiceBrowser:didRemoveService:moreComing:
[self.foundServices removeObject:aNetService];
但是,返回的服务不会被 NetServiceBrowser 保留,因此 didRemoveService
中给出的服务与数组中的服务不是同一对象。如何比较服务以确保我删除的服务是正确的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想得太多了,在你的头脑中制造了一个不存在的问题。对传递到
didRemoveService:
的对象使用removeObject:
。removeObject:
根据对象对isEqual:
的响应进行删除,而不是根据对象的地址或身份。所以这会起作用。答案确实很简单:
Apple 对
removeObject:
的描述解释了这一点:(为了完整起见,Apple确实提供了一个通过地址删除对象的函数。这就是
removeObjectIdenticalTo:
。但是,这不是您想要的行为。只需使用removeObject :
。)参考文献:
You're over-thinking this, and creating a problem in your head that doesn't exist. Use
removeObject:
on the object passed intodidRemoveService:
.removeObject:
removes based on the object's response toisEqual:
, not the address or identity of the object. So this will just work.The answer really is as simple as:
Apple's description of
removeObject:
explains this:(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 useremoveObject:
.)References:
简短的回答,我最终不得不使用这个,但我无法再重现这个问题。使用所选答案。
Short answer, I ended up having to use this, but I can't reproduce the problem anymore. Use the selected answer.