NSEnumerator什么时候完成?

发布于 2024-09-01 23:06:30 字数 508 浏览 8 评论 0原文

我们如何知道枚举何时完成?文档说:

nextObject

当所有对象都被枚举时,返回值为nil。我希望实现一些“类似委托”的行为,从而......

if (nextObject == nil) { 
    do something because we're done!
}

但我发现没有这样的事情:

enumerationDidFinish:

在下面的块中我可以在哪里检查枚举器是否完整?

NSArray *anArray = // ... ;
NSEnumerator *enumerator = [anArray objectEnumerator];
id object;

while ((object = [enumerator nextObject])) {
    // do something with object...
}

How do we know when enumeration is finished? The docs say: the return value of

nextObject

is nil when all objects have been enumerated. I was hoping to implement some "delegate-like" behavior whereby ...

if (nextObject == nil) { 
    do something because we're done!
}

But I see there is no such thing as:

enumerationDidFinish:

where in the following block could I check for the enumerator to be complete?

NSArray *anArray = // ... ;
NSEnumerator *enumerator = [anArray objectEnumerator];
id object;

while ((object = [enumerator nextObject])) {
    // do something with object...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

撩动你心 2024-09-08 23:06:31

只需将代码放在整个 while 块之后即可。

然后当枚举完成后,它就会执行,你就会知道它已经到达末尾了。

Just put your code after the whole while block.

Then when the enumeration is done, it will execute, and you will know it has reached the end.

灰色世界里的红玫瑰 2024-09-08 23:06:31

nextObject 返回的值为 nil 时,枚举器结束

The enumerator finishes when the value returned from nextObject is nil

谜泪 2024-09-08 23:06:31

在 while() 循环之后立即执行怎么样?当nextObject返回nil时,枚举完成,循环条件失败,循环后立即继续执行
身体。

How about immediatley after the while() loop. When nextObject returns nil, the enumeration is complete and the loop condition fails, continuing execution immediately after the loop
body.

触ぅ动初心 2024-09-08 23:06:31

因为如果返回的“对象”为 nil,则 while 循环将不会在主体中继续执行,它将中断到循环末尾,将您想要对对象执行的任何操作都放在那里将是一个好主意。

Since if the returned "object" is nil, the while loop won't continue execution in the body, it will break to the end of the loop, putting whatever you want to do with your object there would be a good idea.

我三岁 2024-09-08 23:06:30

while 循环结束时,您就知道枚举已完成。然后你可以调用委托方法。

When the while loop finishes, you know the enumeration is complete. You could call the delegate method then.

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