0

我正在遍历字典的结果,第一个键是“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);

    }

    }
4

2 回答 2

2

正如 JoshL 提到的那样:

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

key = [NSString stringWithFormat:@"%@%d", rootKey, i];
于 2012-04-27T18:11:25.307 回答
2

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

于 2012-04-27T18:09:15.317 回答