NSEnumerator什么时候完成?
我们如何知道枚举何时完成?文档说:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需将代码放在整个
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.
当
nextObject
返回的值为nil
时,枚举器结束The enumerator finishes when the value returned from
nextObject
isnil
在 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.
因为如果返回的“对象”为 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.
当
while
循环结束时,您就知道枚举已完成。然后你可以调用委托方法。When the
while
loop finishes, you know the enumeration is complete. You could call the delegate method then.