简体   繁体   中英

how to check status inside NSMutableArray

I have 3 NSMutableArray . I want compare two NSMutableArray of three and find different index and store in another NSMutableArray . I want print this sentence when dont save in third NSmutableArray .

this is my code but its not working:

            NSMutableArray *jsonArray = [[NSMutableArray alloc]init];
            jsonArray = [dic objectForKey:@"Modified"];
            NSMutableArray *sqliteArray = [[NSMutableArray alloc]init];
            [sqliteArray addObjectsFromArray:ModiArray];
            differentValue = [[NSMutableArray alloc]init];
    //guys jsonArray and SqliteArray not different index
        if ([jsonArray count] == [sqliteArray count])
            {
                for (int i = 0; i < [sqliteArray count]; i++)
                {
                    if (![jsonArray[i] isEqual:sqliteArray[i]])
                    {
                        [differentValue addObject:[NSNumber numberWithInt:i+1]];
                    }
                }
//My problem here!!! when I NSLog differentValue show me empty but when I write this code dont work
                if (differentValue == NULL)
                {
                    NSLog(@"this array is null");
                }
            }

There's a difference between a null* array and an empty array, just like there's a difference between a blank shopping list and not having a piece of paper. If you want to test if the array is empty, the easiest way is to say if(differentValue.count == 0) .

(mayuur is also correct that you can replace the whole loop with a call to -containsObject: , but there's nothing wrong with your code; it's just not using the frameworks as well as it could.)

* Actually, nil is slightly preferred for objects, because it's declared as an id , but it doesn't matter in this code.

if(![jsonArray containsObject:yourObject])
{
    [differentValue addObject:yourObject];
}

Also note that you are doing the below thing, which is leaking memory :

NSMutableArray *jsonArray = [[NSMutableArray alloc]init]; // NOT RELEASED
jsonArray = [dic objectForKey:@"Modified"];

instead you could directly assign your array like :

NSMutableArray *jsonArray = [dic objectForKey:@"Modified"];

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