while循环添加char而不是添加

时间:2012-04-27 17:58:16

标签: iphone objective-c ios5

我循环查看字典的结果,第一个键是“TC”,下一个键是“TC2”,依此类推。下面代码的while语句循环到TC1然后循环到TC12。它正在添加一个字符而不是在i上添加。如何通过循环向每个TC添加1。

示例:TC1,TC2,TC3等

NSString *key = @"TC";

dict = [resp objectForKey:key];
NSString* cNum = [dict objectForKey:@"CSHR_NUM"];
NSString* usrn = [dict objectForKey:@"CSHR_POS_NAME"];
NSLog( @"First for Cashier Num=%@, Cashier Name=%@", cNum, usrn);

if (cNum  != IDNUM) {

   // key = [NSString stringWithFormat:@"%@%d", key, i];
    NSLog(@"KEY after if %@", key);
    dict = [resp objectForKey:key];
    cNum = [dict objectForKey:@"CSHR_NUM"];
    usrn = [dict objectForKey:@"CSHR_POS_NAME"];
            NSLog(@"c1= %@ , %@", cNum, usrn);

    while (cNum  != NULL) {
        int i = 0;
        NSLog(@"Key on While %@", key);
        i++;


        key = [NSString stringWithFormat:@"%@%d", key, i];
        dict = [resp objectForKey:key];
        cNum = [dict objectForKey:@"CSHR_NUM"];
        usrn = [dict objectForKey:@"CSHR_POS_NAME"];


        NSLog(@"c2= %@ , %@", cNum, usrn);

        NSLog(@"KEY %@", key);

    }

    }

2 个答案:

答案 0 :(得分:2)

保留原始“root”键的值,以便您可以在每次迭代时为其添加新数字。然后,不要在stringWithFormat调用中使用“key”,而是使用“rootKey”。

答案 1 :(得分:2)

正如JoshL提到的那样:

NSString *rootKey = @"TC";
....

key = [NSString stringWithFormat:@"%@%d", rootKey, i];