Iphone 在不工作的情况下顶住
我对以下代码有一个奇怪的问题:Iphone 在不工作的情况下顶住,iphone,objective-c,ios,while-loop,Iphone,Objective C,Ios,While Loop,我对以下代码有一个奇怪的问题: 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",
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只在第一次被调用
为了让它更清楚一点,整个代码运行良好,但只要我是++到1。未调用if语句。它看起来像是ios i在第一次之后出于性能原因或诸如此类的原因取消了它。有人有什么想法吗
Thnx 内部循环第一次完成后,o计数器等于数组的计数,因此它不会再次进入循环。要使其工作,必须在外循环的每次迭代中重置o计数器:
while (i<c){
o = 0;
while (t > o)
...
Edit2:您还可以通过使用NSPredicate筛选阵列来消除第二个循环:
for (NSString *searchId in whatsNewArray1){
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"productId == %@",searchId];
[finalWhatsNew addObjectsFromArray:[dames filteredArrayUsingPredicate:predicate]];
}
正如Vladimir所说,您需要在外部循环的每次迭代之后重置o。理想情况下,您可以在此处切换到for语句,因为它们完全适合您的工作:
for (int i=0; i<c; ++i) {
// ...
for (int o=0; o<t; ++o) {
// ...
whatsNewArray1中有多少对象?您是否可以添加更多的NSLog语句以了解发生了什么并发布输出?
for (int i=0; i<c; ++i) {
// ...
for (int o=0; o<t; ++o) {
// ...