NetServiceBrowser 删除后会提供不同的 NetService

发布于 2024-12-20 15:08:39 字数 413 浏览 1 评论 0原文

我希望做这样的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2024-12-27 15:08:39

你想得太多了,在你的头脑中制造了一个不存在的问题。对传递到 didRemoveService: 的对象使用 removeObject:removeObject: 根据对象对 isEqual: 的响应进行删除,而不是根据对象的地址或身份。所以这会起作用。

答案确实很简单:

[self.foundServices removeObject:aNetService];

Apple 对 removeObject: 的描述解释了这一点:

此方法使用 indexOfObject: 来定位匹配项,然后使用 removeObjectAtIndex: 删除它们。因此,匹配是根据对象对 isEqual: 消息的响应来确定的。

(为了完整起见,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 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:

因为看清所以看轻 2024-12-27 15:08:39

简短的回答,我最终不得不使用这个,但我无法再重现这个问题。使用所选答案。

- (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];
    }
}

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文