S-T-R-E-G-A
S-T-R-E-G-A

Reputation: 277

Multiple statements in a loop with if else

EDIT: Updated code to better reflect my problem

this code returns 9 strings to badDestination1

NSMutableArray* goodDestination1 = [NSMutableArray array];
NSMutableArray* badDestination1 = [NSMutableArray array];
NSMutableArray* badDestination2 = [NSMutableArray array];

for (NSString* item in sourceArray)
{
    if ([item rangeOfString:@"<b>"].location != NSNotFound)

        [goodDestination1 addObject:item];

    else {
        [badDestination1 addObject:item];
        //[badDestination2 addObject:@"Title"];
    }
}

This code returns 1 value to badDestination2

for (NSString* item in sourceArray)
    {
        if ([item rangeOfString:@"<b>"].location != NSNotFound)

            [goodDestination1 addObject:item];

        else {
            //[badDestination1 addObject:item];
            [badDestination2 addObject:@"String"];
        }
    }

anyone know whats going on? Seems like the "String" might be getting rewritten in the same location on the array maybe?

Upvotes: 3

Views: 219

Answers (1)

Michael Myers
Michael Myers

Reputation: 191905

Looks like you're missing the braces {} after the else.

else {
  [arrayDestinationBad1 addObject:item]; 
  [arrayDestinationBad2 addObject:@"String"]; 
}

Upvotes: 5

Related Questions