2

I have this section of code here:

for (int i = 0; i<[taskData count]; i++)
    {
        for (int j=0; j<[allJobTaskArray count]; j++)
        {
            NSLog(@"%d", i);
            NSLog(@"%d", j);
            PunchListDataCell *pldCell = [[[PunchListDataCell alloc]init] autorelease];
            pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
            pldCell.cellSelected = NO;
            [punchListData addObject:pldCell];
        }

    }

Now let me explain:

  • taskData count is 57 and is a NSArray
  • allJobTaskArray count is 12 and is a NSMutableArray
  • This code will crash at this line: pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]]; when j is 6 and i is 36 simple because in allJobTaskArray objectAtIndex: 6 objectAtIndex: 36 does not exist.
  • This is the error I am getting: [__NSCFArray objectAtIndex:]: index (36) beyond bounds (36)
  • What I am trying to do is if the item does not exist, then pldCell should equal @"";
    • My question is how would I check if I am item exist or not ?

I have tried the following:

if([[allJobTaskArray objectAtIndex:j] objectAtIndex:i] == [NSNull null]){
                pldCell.stringData = @"";
            }else{
                pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
            }
0

1 Answer 1

1

Altogether it should look something like -

for (int i = 0; i<[taskData count]; i++)
    {
        for (int j=0; j<[allJobTaskArray count]; j++)
        {
            NSLog(@"%d", i);
            NSLog(@"%d", j);


            PunchListDataCell *pldCell = [[[PunchListDataCell alloc]init] autorelease];
            if ([[allJobTaskArray objectAtIndex:j] count] > i) {                    
                pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
            } else {
                pldCell.stringData = @"";
            }

            pldCell.cellSelected = NO;
            [punchListData addObject:pldCell];
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

nope, still crashes, it always goes into the else I added which is pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]];
It should actually be >= i, sorry @user979331
Can you update the question with your current code? I updated my answer with the complete loops @user979331
Turns out this works...I just had pldCell.stringData= [self reverseStringDate:[[[allJobTaskArray objectAtIndex:j] objectAtIndex:i] substringToIndex:10]]; before the condition.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.