按一定顺序将对象添加到数组中?
我有 4 个带有完成块的异步网络请求。完成后,我希望将它们添加到主 NSMutableArray 中,这是请求发送的顺序,而不是接收的顺序(因为这可能不同)。我在发送请求时设置了一个标签,这样我就可以在完成块中使用这个标签。
I have a 4 async network requests with a completion block. Upon completion, i want them to be added to a master NSMutableArray
is the order that the requests were sent, but not the order they were received (as this may be different). I have a tag that I set when sending the request so I can use this tag in the completion block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最初用四个 NSNull 填充数组。在处理程序中,
[yourArray ReplaceObjectAtIndex:tag withObject:responseObject]
。Fill the array with four NSNulls initially. In the handler,
[yourArray replaceObjectAtIndex:tag withObject:responseObject]
.我假设您不知道每个请求将返回多少个元素,因此您必须暂时存储所有四个结果集,直到所有四个请求完成。 (一个由标签作为键的 NSMutableDictionary 是有意义的。)然后,您可以迭代字典的键并以正确的顺序将这些 objectForKeys (数组)附加到可变数组中。
I assume you don't know how many elements each request will return, so you'll have to store all four of your result sets temporarily until all four requests are finished. (An NSMutableDictionary, keyed by the tag would make sense.) Then, you can iterate through the dictionary's keys and append those objectForKeys (arrays) to your mutable array in the correct order.