0

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.

6 Answers 6

3

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.
        } 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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]];
    }
}

6 Comments

Great solution, but this will only work with simple object like, string and numbers. Not with more complex object.
Can You provide an example of more complex object? I'm using this method in my own project to check in array and find location of NSIndexPath objects, various string and float values.
Wel complex object could be object you created your self. Like a class that extents NSObject with custom properties. The contains object method will call the isEqual: method on the objects in the array: developer.apple.com/library/ios/#documentation/Cocoa/Reference/…:
Fair enough - then my example would fail. But anyways - I believe we should use simple methods for simple things, and more complex, for complex ones.
Totally agree, never make you code more complex and it needs to be.
|
1

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 :)

Comments

0
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
        }
    }

Comments

0

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");
        }
    }

Comments

0

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

[array1 isEqualToArray:array2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.