学习 - For和If陈述

时间:2014-09-16 01:05:45

标签: objective-c

我正在从三个文件中的信息创建几个数组。我想使用每个文件中的一个关键对象,并将每个文件的一些元素组合成数组,其中每个数组中的每个索引位置都包含与关键项相关联的数据。

我正在尝试遍历每个文件,查找匹配的键值,然后在索引位置创建数组对象,以便所有相关信息排列。我已经尝试了几种方法,NSLogged输出以查看是否有相关数据,但我的程序停在"如果"语句和数据不在所需的数组索引中。如果我能解决" pvSer"对象,我相信其余的会自然而然地跟随。

for (t =0; t < [myInv count]; t++){
    compScott = [vScott objectAtIndex:t];
    //NSLog(@"comparison Scott Number = %@", compScott);

    //NSLog(@" ListPrice count = %lu", [listPrice count]);
    for (p = 0; p < [listPrice count]; p++){

            NSLog(@"comp Scott Number = %@", compScott);
            NSLog(@"   Loop iterations t and p = %d and %d", t,p);
            NSLog(@"pvScott Number at Index p: %@ at index %i", [pvScott  objectAtIndex:p],p);
            NSLog(@"===============================================================");
            NSLog(@" pvSer value = %@  at Index p %d", [pvSer objectAtIndex:p], p);
            NSLog(@" pvNew value = %@  at Index p %d", [pvNew objectAtIndex:p], p);
            NSLog(@" pvUsed value = %@  at Index p %d", [pvUsed objectAtIndex:p], p);
            NSLog(@" pvPlate value = %@  at Index p %d", [pvPlate objectAtIndex:p], p);
            NSLog(@" pvSht value = %@  at Index p %d", [pvSht objectAtIndex:p], p);

        NSLog(@" vScott at Index T: %@, %d and pvScott at Index P: %@, %d", [vScott objectAtIndex:t],t,[pvScott objectAtIndex:p],p);

        NSLog(@"====================================================================");

        if ([vScott objectAtIndex:t] == [pvScott objectAtIndex:p]){
                //compVSer = [pvSer objectAtIndex:p];
            [vSerPrice insertObject:[pvSer objectAtIndex:p] atIndex:t];
                NSLog(@" the value of pvSer is %@ at Index (p) %d", [pvSer objectAtIndex:p],p);
                NSLog(@" the value vSerPrice is: %@ at Index (t) %d", [vSerPrice objectAtIndex:t],t);

        NSLog(@"====================================================================");

        }
    }

我的输出部分与标准相关如下:

2014-09-15 20:43:19.140 Stamp Collection[5616:303] comp Scott Number = 4435
2014-09-15 20:43:19.140 Stamp Collection[5616:303]    Loop iterations t and p = 0 and 4
2014-09-15 20:43:19.140 Stamp Collection[5616:303] pvScott Number at Index p: 4435 at index 4
2014-09-15 20:43:19.141 Stamp Collection[5616:303] ====================================================================
2014-09-15 20:43:19.141 Stamp Collection[5616:303]  pvSer value = 0.00  at Index p 4
2014-09-15 20:43:19.141 Stamp Collection[5616:303]  pvNew value = 0.95  at Index p 4
2014-09-15 20:43:19.141 Stamp Collection[5616:303]  pvUsed value = 0.25  at Index p 4
2014-09-15 20:43:19.141 Stamp Collection[5616:303]  pvPlate value = 0.00  at Index p 4
2014-09-15 20:43:19.141 Stamp Collection[5616:303]  pvSht value = 12.00  at Index p 4
2014-09-15 20:43:19.141 Stamp Collection[5616:303]  vScott at Index T: 4435, 0 and pvScott at Index P: 4435, 4
2014-09-15 20:43:19.141 Stamp Collection[5616:303] ====================================================================

1 个答案:

答案 0 :(得分:0)

你没有提到错误,所以我假设你的if语句总是返回false。我相信这是因为数组返回一个字符串,所以==比较指针而不是值,它们在你的情况下永远不会相同。请尝试isEqualToString

if ([[vScott objectAtIndex:t] isEqualToString: [pvScott objectAtIndex:p]]){