Is IBOutletCollection guaranteed to be of correct order?

前端 未结 4 482
借酒劲吻你
借酒劲吻你 2020-12-09 08:35

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 signifi

4条回答
  •  情话喂你
    2020-12-09 08:51

    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.

    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.

提交回复
热议问题