Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it documented anywhere that the array `instantiateWithOwner` returns is ordered in a specific way?

I've lately seen some code sample that used instantiateWithOwner to load the cells for a UITableView this way:

cell = [nib instantiateWithOwner:self options:nil][0];

while it's cleaner and more compact than having a property outlet as a prototype cell, I wonder if it's guaranteed(aka documented) that the first top level object in the interface builder will always be the first in the array returned by instantiateWithOwner. It may be the case at this point, but if not documented(undefined), it could change in the future, causing trouble for nibs that have more than one top level object and instantiate cells like this.

like image 268
Rad'Val Avatar asked Jan 06 '13 22:01

Rad'Val


1 Answers

I can't find anywhere that documents the order of the array returned by instantiateWithOwner specifically, but the documentation does talk about the order that nib-loading code calls awakeFromNib:

The order in which the nib-loading code calls the awakeFromNib methods of objects is not guaranteed.

My assumption is that this means the order of the array returned by instantiateWithOwner is also not guaranteed. To guarantee order when pulling multiple objects out of a xib, I usually start by configuring each object's tag through interface builder with a unique (ascending) number. Then, your code would look something like this:

NSArray *cells = [[nib instantiateWithOwner:self options:nil] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]];
cell = cells[0];
like image 113
wxactly Avatar answered Nov 14 '22 23:11

wxactly