开发者

How to compare each object of one NSMutableArray with other

开发者 https://www.devze.com 2023-03-26 17:07 出处:网络
In arr3 I want the index of the object from arr1 when arr1[k]==arr2[m]. However, I am not getting the value in arr3 because it must not satisfy if condition.

In arr3 I want the index of the object from arr1 when arr1[k]==arr2[m].

However, I am not getting the value in arr3 because it must not satisfy if condition.

How can I compare two NSMutableArray objects by their indices?

while (k<=[arr1 count]-1 ) {
    for (m=0; m<=[arr2 count]-1; m++) {

    if ([arr1 objectAtIndex:k]=开发者_JAVA百科=[arr2 objectAtIndex:m]) {
        [arr3 addObject:k];
    }
    }
    k++;
}


NSMutableArray *arr1,*arr2,*arr3;

arr1=[[NSMutableArray alloc] initWithObjects:@"vijay",@"india",@"AppleVijay@facebook.com",nil];

arr2=[[NSMutableArray alloc] initWithObjects:@"vijay",@"AppleVijay@facebook.com",nil];

arr3=[[NSMutableArray alloc] init];


    //Note: Here u have to properly create those arr1,arr2,arr3 arrays with alloc and init and also add objects for arr1,arr2 for compare.


for (id obj in arr2) {//each obj in arr2


    if ([arr1 containsObject:obj]) {//if arr1 has the same obj(which is from arr2)

        [arr3 addObject:[NSNumber numberWithInteger:[arr1 indexOfObject:obj]]];//then add that indexofobject to arr3

    }

}


NSLog(@"resulted arr3 : %@ \n\n",arr3);

OUTPUT:

resulted arr3 : (

                 0,

                 2

                 ) 
0

精彩评论

暂无评论...
验证码 换一张
取 消