带有if else的循环中的多个语句

时间:2010-02-09 15:48:40

标签: objective-c

编辑:更新了代码以更好地反映我的问题

此代码将9个字符串返回给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"];
    }
}

此代码将1个值返回给badDestination2

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

            [goodDestination1 addObject:item];

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

任何人都知道发生了什么事? 似乎“String”可能会在阵列的相同位置被重写?

1 个答案:

答案 0 :(得分:5)

您似乎错过了{}之后的大括号else

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