简体   繁体   中英

Compare the objects of NSMutablearray with For loop values

I have the NSMutableArray with following values:

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

This is my For loop:

for(int i=0; i<30; i++)
{
    //here I want to compare each object from above array with value of i from for loop. and add the further output. e.g.
    if(i== object from array)
    {
          //do this
    }
}

Actually I have just five objects or values in array so how can I compare each value of i with each object or values of NSMutableArray.

So if I understand you correctly you want to compare each object in the array with all other objects in the Array? Here is example, not test and might need some optimalisation.

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

for(NSInteger i = 0; i < [array count]; i++) {
    for(NSInteger j = 0; j < [array count]; j++) {
        if ( i == j) {
           // No need to check if its the same object.
           continue;
        }

        NSString *stringI = [array objectAtIndex:i];
        NSString *stringJ = [array objectAtIndex:j];

        if ([stringI isEqualToString:stringJ) {
           // Do something.
        } 
    }
}

If you trust IOS functions containsObject and indexOfObject , then:

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

for(int i = 0; i< 30; i++)
{
    if([array containsObject:[NSString stringWithFormat:@"%i",i]])
    {
        //array contains  "i" item,  
        //and we can know it's location this way:
        int foundArrayItemId = [array indexOfObject:[NSString stringWithFormat:@"%i",i]];
    }
}

just use nested loop for comparison like

for(i = 0; i <= [array count];i++)
    {
        for(j=0; j<=[mutableArrya count]; j++)
            {
                 //Do comparison
            }
    }

i think this will help you

Happy Coding :) enjoy it :)

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

for(int i=0;i<30;i++)

    {
      NSString str= [NSString stringWithFormat:@"%i",i];
        if([array containsObject:str])
        {
          //do this
        }
    }

Write this code :

NSMutableArray *arrCount = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30", nil];
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"21",@"12",@"23",@"14",@"25", nil];

    for (NSString *strComapre in array)
    {
        NSLog(@"%@",strComapre);
        if ([arrCount containsObject:strComapre]) 
        {
             NSLog(@"Compare");
        }
        else
        {
             NSLog(@"Not Compare");
        }
    }

There is an isEqualToArray method in NSArray. Can you not use that as follows:

[array1 isEqualToArray:array2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM