tags:

views:

53

answers:

1

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?

+5  A: 

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

else {
  [arrayDestinationBad1 addObject:item]; 
  [arrayDestinationBad2 addObject:@"String"]; 
}
Michael Myers
He says it only gets called once (obviously implying he wants it called more often). Without the braces it /should/ get called every time through the loop.
Matthew Flaschen
@Matthew: That's true, but what else could be the problem with that code?
Michael Myers
I don't know, but adding the braces can only make it run less (<=) often. :)
Matthew Flaschen
Perhaps it was mis-copied and that statement is actually *outside* the loop. That's the only thing I can think of.
Michael Myers
for (NSString* item in doesntContainAnother2) { if ([item rangeOfString:@"<b>"].location != NSNotFound) { [containsAnother3 addObject:item]; } else { [doesntContainAnother3 addObject:item]; [doesntContainAnother3Titles addObject:@"Title"]; } }That still only calls it once. Is it possible my string is overwriting itself in the array?
S-T-R-E-G-A
How many items are in the source array?
Michael Myers
it can vary, but the source is a really big string. I am using this as a method to weed out HTML tags to make parsing simple.
S-T-R-E-G-A
@S-T-R-E-G-A *(and by the way, that name is a pain to type ;) )* : Can you use a debugger and step through the code? That is likely to be much more revealing than anything we tell you. If not, how are you checking the destination array afterwards? Are you certain it only has one element?
Michael Myers
@mmyers I think your right. I may have jumped to conclusions. with nslog showing array.count on both arrays I return values from 9 (what I want) and 1092 (no idea where that is comming from...). I will study what I am doing a little more!
S-T-R-E-G-A
figured it out... I was writing the "String" to a key value in a dictionary of a plist so it just wrote the same key over and over updating the value. Result was 1 key with the last value. :(How embarrassing... but I figured it out. Thanks for the assistance guys!
S-T-R-E-G-A
So it was a dictionary and not an array? Tsk, tsk. ;)
Michael Myers