IBOutletCollection是否保证是正确的顺序? [英] Is IBOutletCollection guaranteed to be of correct order?

查看:433
本文介绍了IBOutletCollection是否保证是正确的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用NSArray的IBOutletCollection,我有两个矛盾的来源。
首先说你将东西从接口到出口集合的顺序很重要,并且它在出口集合中总是相同的。例如。如果我们首先将第一个文本字段链接到集合,则元素编号0将是该字段等。

I have two contradicting sources in regards to IBOutletCollection with NSArray. First says that the order that you link things from interface to outlet collection is significant, and it will always be the same in outlet collection. E.g. if we link first text field first to collection, element number 0 will be that field, etc.

但另一个来源告诉我这是错误的并且实际上Outlet集合是随机的并且绝对不能保证事情的顺序。因此元素数字零可以是任何文本字段,而不仅仅是在界面中连接的第一个。

But another source tells me that this is wrong and actually Outlet collection is randomized and there is absolutely no guarantee in the order of things. So element number zero can be any text field, and not just the first connected in interface.

iOS 6编程 - 探索iOS SDK是第一个源和斯坦福课程在iTunes上是第二个来源。
谁是对的?

"iOS 6 Programming - exploring iOS SDK" is the first source and Stanford course on iTunes is the second source. Who is right?

推荐答案

两种来源都是正确的:一方面,由于实施细节接口生成器,在 IBOutletCollection 中添加项目的顺序将在检索时保留;另一方面,你应该避免在你的代码中使用那个顺序,因为没有办法检查这个顺序。

Both sources are sort of right: on one hand, due to the implementation details of the Interface Builder, the order in which you add items to IBOutletCollection will be maintained on retrieval; on the other hand, you should avoid making use of that ordering in your code, because there is no way to check this order.

想象一下接管别人的项目。如果您看到 IBOutletCollection 上的循环,请注意迭代的顺序很重要,并决定检查该订单是什么或强制新订单,您必须删除和将控件重新添加到您的插座集合中。这就是为什么你应该将 IBOutletCollection 元素视为无序的原因。如果需要维护特定的顺序,请将元素复制到 NSArray 中,对某些已知属性进行排序,然后迭代复制的集合。

Imagine taking over someone else's project. If you see a loop over an IBOutletCollection, observe that the order of iteration matters, and decide to check what that order is or force the new order, you would have to remove and re-add the controls to your outlet collection. That is why you should treat your IBOutletCollection elements as unordered. If it is necessary to maintain a specific order, copy the elements into an NSArray, sort them on some known property, and then iterate the copied collection.

这篇关于IBOutletCollection是否保证是正确的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆