USK
USK

Reputation: 499

Comparing two NSNumber objects

I used this code to compare two NSNumber objects, but it never passed the if condition.

listItems = [appDelegate.productStatus componentsSeparatedByString:@","];


for (int i=0;i<[appDelegate.productArray count]; i++)
{

    for (int j=0; j<[listItems count]; j++) 
    {
        number=[NSNumber numberWithInt:[[listItems objectAtIndex:j] intValue]];
        NSLog(@"number %@",number);
        productObject=[appDelegate.productArray objectAtIndex:i];           
        NSLog(@"%@,%@",productObject.pid,number);
        if (productObject.pid == number) 
        {
            NSLog(@"BUY it!!!");
            [purchasArray addObject:productObject];
        }

    }
}

What is wrong?

Upvotes: 28

Views: 36822

Answers (4)

Saad
Saad

Reputation: 8947

My sugestion is to compare it as

if([productObject.pid intValue] == [number intValue])
{
 NSLog(@"BUY it!!!");
        [purchasArray addObject:productObject];
}

cheers.

I'd avoid object comparison

Upvotes: 50

Deviator
Deviator

Reputation: 688

Try compare method instead of '=='.

if([1stNum compare:secNum] == NSOrderedSame) 
  {
      // do something
  }

Tell me if it helps now!

Upvotes: 13

vishiphone
vishiphone

Reputation: 750

Use like this I thing this will helpful for you

NSNumber *n=[[NSNumber alloc]init];
if([n isEqualToNumber:somenumber])
 {
 }

Upvotes: 2

Nitin
Nitin

Reputation: 7461

Change following code..

if ([productObject.pid isEqualToNumber number]) 
    {
        NSLog(@"BUY it!!!");
        [purchasArray addObject:productObject];
    }

Hope, this will help you..

Upvotes: 30

Related Questions