My code is the following,
for (int i=0; i < kNumberOfTitles; ++i) {
UILabel * aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, i*10, 100, 30)];
[_titles addObject:aLabel];
NSAssert([_titles objectAtIndex:0] != nil, @"wrong 1");
}
NSAssert([_titles objectAtIndex:0] != nil, @"wrong 2");
((UILabel *)[_titles objectAtIndex:0]).text = @"Tel";
((UILabel *)[_titles objectAtIndex:1]).text = @"Add";
for (UILabel* aLabel in _titles) {
[self.view addSubview:aLabel];
}
Before first NSAssert, I just add an new label and then retrieve it, I found it’s nil.
What’s the problem?
I suspect the problem is that you’re not actually creating the array so it’s
nilif it’s an instance variable (or you use ARC) or some garbage value if it’s a simple local variable if you don’t use ARC. You must dobefore trying to add items to the array.