Question

I have an NSArray that I pass in through a delegate method:

- (void)didFinishParsing:(NSArray *)array {
    [self.myArray addObjectsFromArray:array];
    NSLog(@"%@", self.myArray);
    [self.tableView reloadData];
}

There's only one object in the array and I'd like the tableView row count to equal the amount of objects in the array objectAtIndex:0, but I cannot get the count. I have tried:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.myArray objextAtIndex:0] count];
}

How do I get the number of object inside the first (and only) object in my array?

Was it helpful?

Solution

To count the array at index 0 of self.myArray your syntax is correct. Is your array variable setup how you think it is?

The method call addObjectsFromArray: will add the contents of array to the end of self.myArray.

So in other words: [array objectAtIndex:0] will become [self.myArray lastObject:]

If you're having trouble running count: on [self.myArray objectAtIndex:0] or [self.myArray lastObject] then the object you're trying to count might be nil, empty, not an NSArray/Set as you are expecting it to be.

OTHER TIPS

If you're trying to create an array within an array you should instead use

[self.myArray addObject: array];

This will add the array at the next index inside self.myArray. What you were doing previously with

[self.myArray addObjectsFromArray:array];

was adding the contents of array into self.myArray not adding array itself into self.myArray.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top