目标C循环不循环

时间:2021-02-21 19:55:51

I have a while loop that should be looping through a condition, but fails to go through the process more than once.

我有一个循环应该循环一个条件,但不能多次完成该过程。

Here is my relevant code:

这是我的相关代码:

NSInteger i = 0;
    while(i <= building.departments.count)
    {
        NSLog(@"Number of building departments: %lu", (unsigned long)building.departments.count);
        NSLog(@"i : %ld", (long)i);

        UILabel *dtitleLB = [[UILabel alloc] initWithFrame:CGRectMake(5, y, 310, 20)];
        dtitleLB.text = ((DBDepartment*)[building.departments objectAtIndex:i]).Name;
        dtitleLB.textAlignment =  UITextAlignmentLeft;
        dtitleLB.backgroundColor = [UIColor clearColor];
        dtitleLB.textColor = [UIColor lightGrayColor];
        dtitleLB.font = [UIFont fontWithName:@"Helvetica" size:(16.0)];
        [scrollView addSubview:dtitleLB];
        y += 30;

        UIButton* dphoneNB = [UIButton buttonWithType:UIButtonTypeCustom];
        [dphoneNB setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [dphoneNB setTitleColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.9 alpha:1.0] forState:UIControlStateNormal];
        [dphoneNB addTarget:self action:@selector(numberPress:) forControlEvents:UIControlEventTouchUpInside];
        [dphoneNB setTitle:((DBDepartment*)[building.departments objectAtIndex:i]).Phone forState:UIControlStateNormal]; i++;
        dphoneNB.frame = CGRectMake(5, y, 315, 25);
        [scrollView addSubview:dphoneNB];
        y += 30;

        UIButton* dwebsiteNB = [UIButton buttonWithType:UIButtonTypeCustom];
        [dwebsiteNB setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [dwebsiteNB setTitleColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.9 alpha:1.0] forState:UIControlStateNormal];
        [dwebsiteNB addTarget:self action:@selector(linkPress:) forControlEvents:UIControlEventTouchUpInside];
        [dwebsiteNB setTitle:((DBDepartment*)[self->building.departments objectAtIndex:i - 1]).Website forState:UIControlStateNormal]; i++;
        dwebsiteNB.frame = CGRectMake(5, y, 315, 25);
        [scrollView addSubview:dwebsiteNB];
        y += 30;

        i++;
    }

The output for the first NSLog = 1 or greater depending on the user selection (never 0). The output for the second NSLog = 0 and never gets larger.

第一个NSLog的输出= 1或更大,具体取决于用户选择(从不为0)。第二个NSLog的输出= 0并且永远不会变大。

If you could go over my code and see if there are any obvious errors, that would be greatly appreciated.

如果你可以查看我的代码,看看是否有任何明显的错误,那将非常感激。

Thanks!

谢谢!

1 个解决方案

#1


1  

The reason it is not looping (or looping erratically) is because you increment i in more than one spot. See the lines below (broken up to see it easier).

它不循环(或循环不规则)的原因是因为你在多个点增加i。请参阅下面的行(分解以便更容易看到)。

...
        [dphoneNB setTitle:((DBDepartment*)[building.departments 
        objectAtIndex:i]).Phone forState:UIControlStateNormal]; i++;
...
        [dwebsiteNB setTitle:((DBDepartment*)[self->building.departments 
        objectAtIndex:i - 1]).Website forState:UIControlStateNormal]; i++;
...
        i++;

As an alternative to while loops like this, a for loop should suffice.

作为像这样的while循环的替代,for循环应该足够了。

#1


1  

The reason it is not looping (or looping erratically) is because you increment i in more than one spot. See the lines below (broken up to see it easier).

它不循环(或循环不规则)的原因是因为你在多个点增加i。请参阅下面的行(分解以便更容易看到)。

...
        [dphoneNB setTitle:((DBDepartment*)[building.departments 
        objectAtIndex:i]).Phone forState:UIControlStateNormal]; i++;
...
        [dwebsiteNB setTitle:((DBDepartment*)[self->building.departments 
        objectAtIndex:i - 1]).Website forState:UIControlStateNormal]; i++;
...
        i++;

As an alternative to while loops like this, a for loop should suffice.

作为像这样的while循环的替代,for循环应该足够了。