0

I am trying to create an array of numbers that I will use later to determin the size of my tableviewcells.

However I am having some issues with the array, After my while statment it comes back as being NULL, yet when I log the values I am getting from my array of objects they are correct... and the if statement works perfectly.

This is my code

int count = 0;

// Cell heights
int smallCell = 69;
int largeCell = 120;

NSNumber *currentHeight = [[NSNumber alloc] init]; // allows int to be stored into NSArray

while (count < seriesSearchArray.count) {
    myObj = (SeriesSearchResultItem*)[dataArrayOfObjects objectAtIndex:count];

    if (![myObj.seriesNote isEqualToString:@""]) {
        NSLog(@"%@", myObj.seriesNote);
        currentHeight = [NSNumber numberWithInt:largeCell];
        NSLog(@"%@", currentHeight); // correct value shown
        [heightArray addObject:currentHeight];
    }
    else {
        currentHeight = [NSNumber numberWithInt:smallCell];
        NSLog(@"%@", currentHeight); // correct value shown
        [heightArray addObject:currentHeight];
    }


    NSLog(@"%@", heightArray); // NULL Shown

    count ++;
}

So thats if, I am trying to get the value from each of the objects in my array which works, the if statment works perfectly but then when I try to add them to my new array it always comes back as NULL.

3
  • You may need to show more code. I don't see you create (alloc/init) the heightArray. Then again, that could be the problem... Commented Nov 29, 2012 at 1:23
  • I have @property/@synthesized heightArray, because I need to access it from another method after I have put the values into it. Commented Nov 29, 2012 at 1:25
  • 1
    That creates the pointer to it. It doesn't actually create the NSMutableArray. You need something like heightArray = [NSMutableArray arrayWithCapacity:seriesSearchArray.count] Commented Nov 29, 2012 at 1:27

1 Answer 1

1

Moving comment to answer

You need something like

heightArray = [NSMutableArray arrayWithCapacity:seriesSearchArray.count] 
Sign up to request clarification or add additional context in comments.

1 Comment

Similar approaches would be to do [[NSMutableArray alloc] init], [@[] mutableCopy], or to lazily instantiate it by doing any of these options in the getter for the array if it is nil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.