Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 924
USK Avatar asked May 03 '12 09:05

USK


4 Answers

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

like image 171
Saad Avatar answered Nov 20 '22 13:11

Saad


Change following code..

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

Hope, this will help you..

like image 39
Nitin Avatar answered Nov 20 '22 12:11

Nitin


Try compare method instead of '=='.

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

Tell me if it helps now!

like image 13
Deviator Avatar answered Nov 20 '22 12:11

Deviator


Use like this I thing this will helpful for you

NSNumber *n=[[NSNumber alloc]init];
if([n isEqualToNumber:somenumber])
 {
 }
like image 2
vishiphone Avatar answered Nov 20 '22 13:11

vishiphone