不工作时计数器

发布于 2024-11-26 08:08:31 字数 704 浏览 1 评论 0原文

我对以下代码遇到了一个奇怪的问题:

int c = [whatsNewArray1 count];
int t = [dames count];
int i = 0;
int o= 0;
NSMutableArray *finalWhatsNew = [[NSMutableArray alloc]init];
while (i<c){
    NSLog(@"teller array %i", i);

    while(t>o){
        NSLog(@"dames %i", i);
        if ([[[dames objectAtIndex:o] productId] isEqualToString:[whatsNewArray1 objectAtIndex:i]]){
            [finalWhatsNew addObject:[dames objectAtIndex:o]];
            NSLog(@"inner dames%i", i);
        }
        o++;
    }
    i++;
}

此代码从“dames”数组中检索“finalWhatsNew”数组中所述的所有条目。问题在于 if 仅在第一次被调用。

为了更清楚一点,整个代码工作正常,但是一旦“i”++ 到 1,就不会调用 if 语句。看起来 ios 我在第一次后出于性能原因或类似原因取消了它。有人有什么想法吗?

谢谢!

I'm having a strange problem with the following code:

int c = [whatsNewArray1 count];
int t = [dames count];
int i = 0;
int o= 0;
NSMutableArray *finalWhatsNew = [[NSMutableArray alloc]init];
while (i<c){
    NSLog(@"teller array %i", i);

    while(t>o){
        NSLog(@"dames %i", i);
        if ([[[dames objectAtIndex:o] productId] isEqualToString:[whatsNewArray1 objectAtIndex:i]]){
            [finalWhatsNew addObject:[dames objectAtIndex:o]];
            NSLog(@"inner dames%i", i);
        }
        o++;
    }
    i++;
}

This code retrieves al the entries from the "dames" array which are stated in the "finalWhatsNew" array. Problem with this is that the if is only get called the first time.

To make it a little bit clearer, the whole code is working fine, but as soon "i" is ++ to 1. the if statement isn't called. It looks like ios i canceling it out after the first time for performance reason or somethins like that. Anybody has any ideas?

Thnx!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2024-12-03 08:08:31

第一次内循环完成后,o 计数器等于数组的计数,因此不会再次进入循环。为了使其工作,您必须在外循环的每次迭代中重置o计数器:

while (i<c){
    o = 0;   
    while (t > o)
    ...

编辑:为了更清晰的代码(并且可能更好的性能),您可以使用快速枚举而不是通常的枚举for/while 循环:

for (NSString *searchId in whatsNewArray1){
   for (YourObject *obj in dames){
        if ([[obj productId] isEqualToString:searchId])
           [finalWhatsNew addObject: obj];
   }
}

Edit2:您还可以通过使用 NSPredicate 来过滤数组来消除第二个循环:

for (NSString *searchId in whatsNewArray1){
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"productId == %@",searchId];
     [finalWhatsNew addObjectsFromArray:[dames filteredArrayUsingPredicate:predicate]];
}

After inner loop is finished for the first time the o counter is equal to array's count and so it won't enter the loop again. To make it work you must reset o counter on each iteration of the outer loop:

while (i<c){
    o = 0;   
    while (t > o)
    ...

Edit: For clearer code (and probably better performance) you can use fast enumeration instead of usual for/while loops:

for (NSString *searchId in whatsNewArray1){
   for (YourObject *obj in dames){
        if ([[obj productId] isEqualToString:searchId])
           [finalWhatsNew addObject: obj];
   }
}

Edit2: Also you can eliminate 2nd loop by using NSPredicate to filter your array:

for (NSString *searchId in whatsNewArray1){
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"productId == %@",searchId];
     [finalWhatsNew addObjectsFromArray:[dames filteredArrayUsingPredicate:predicate]];
}
好倦 2024-12-03 08:08:31

正如 Vladimir 所说,您需要在每次外循环迭代后重置 o 。理想情况下,您可以在此处切换到 for 语句,因为它们完全适合您正在做的事情:

for (int i=0; i<c; ++i) {
    // ...
    for (int o=0; o<t; ++o) {
        // ...

As Vladimir says, you need to reset o after each iteration of the outer loop. Ideally you switch to for-statements here, as they fit what you're doing exactly:

for (int i=0; i<c; ++i) {
    // ...
    for (int o=0; o<t; ++o) {
        // ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文