Reputation: 117
I have 3 NSMutableArray
. I want compare two NSMutableArray
of three and find different index and store in anotherNSMutableArray
. 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");
}
}
Upvotes: 0
Views: 93
Reputation: 17861
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.
Upvotes: 2
Reputation: 4746
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"];
Upvotes: 1