Reputation: 3
I have done a bit of searching and not really found an answer to my question. The code below is trying to copy the contents of a NSMutable array that contains some objects.
I have tried the code below however when run there is no error, but the new arrays do not get the objects as I would have thought they would.
csvContent is a array that contains objects from parsing a CSV file and the other NSMutable arrays round1, round2 etc have been defined in the header file.
The NSLOG output is correct but the arrays contain no objects.
for(int loopNumber = 0; loopNumber < [csvContent count]; loopNumber++)
{
if ([[[csvContent objectAtIndex:loopNumber] objectAtIndex:1] isEqualToString:@"1"])
{
[round1 addObject:[csvContent objectAtIndex:loopNumber]];
NSLog(@"round 1");
}
if ([[[csvContent objectAtIndex:loopNumber] objectAtIndex:1] isEqualToString:@"2"])
{
[round2 addObject:[csvContent objectAtIndex:loopNumber]];
NSLog(@"round 2");
}
if ([[[csvContent objectAtIndex:loopNumber] objectAtIndex:1] isEqualToString:@"3"])
{
[round3 addObject:[csvContent objectAtIndex:loopNumber]];
NSLog(@"round 3");
}
}
Upvotes: 0
Views: 154
Reputation: 318774
Did you actually create and initialize the arrays for round1
, round2
, and round3
? In other words, make sure they are not nil
when this loop is run.
Also, your code is terribly inefficient. You call [csvContent objectAtIndex:loopNumber]
six times inside the loop. Try this (I'm using i
instead of loopNumber
to save typing):
for (int i = 0; i < csvContent.count; i++) {
NSArray *loopContent = csvContent[i];
NSString *val = loopContent[1];
if ([val isEqualToString:@"1"]) {
[round1 addObject:loopObject];
} else if ([val isEqualToString:@"2"]) {
[round2 addObject:loopObject];
} else if ([val isEqualToString:@"3"]) {
[round3 addObject:loopObject];
}
}
Upvotes: 2