Reputation: 953
NSArray *array=[[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
NSNumber *j = @(1);
for(id i in array) {
if ([i isEqual:j])// always executes an else..I don't know why
{
NSLog(@"This object is matching %@",i);
}
else
{
NSLog(@"This Object is missing %@",i);
}
j = @([j intValue] + 1);
}
the code always executes the else. I don't know why. I want to check the array elements whether some missing in a list or not
Upvotes: 1
Views: 111
Reputation: 19602
You are comparing NSString
s from your array with the NSNumber
j
, so the comparison always fails
One way would be to use the stringValue
of j
instead:
NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
NSNumber *j = @(1);
for(NSString *i in array)
{
if ([i isEqualToString:j.stringValue])
{
NSLog(@"This object is matching %@",i);
}
else
{
NSLog(@"This Object is missing %@",i);
}
}
Upvotes: 1
Reputation: 57114
You are putting strings in your array using @"..."
, but you should put in integers:
NSArray *array=[[NSArray alloc] initWithObjects:@1,@2,@3,@4,@5,@6,@7,@8,@9,@10, nil];
Upvotes: 1