-1

I have a price field which a NSDecimalNumber. I want to create a condition if the value is.

I tried the following:

- (NSString *)numberToPriceString:(NSDecimalNumber *)price{
    NSDecimalNumber *zero = [[NSDecimalNumber alloc] initWithString:@"0"];
    if (price == zero){

    }else{

    }

However the zero value shows a debug value of @"0".

How do I create a zero value equal to something I can catch the condition with?

Atma
  • 29,141
  • 56
  • 198
  • 299

1 Answers1

1

As a rule of thumb, don't use == for comparing objects in Objective-C, unless you're really sure that the pointer values comparison is what you need. In other words, == will return true only if you have the same instance on both sides of the operator.

The usual way to do comparison in Objective-C is to use isEqual: method. However, NSDecimalNumber is a tricky one.

You need to either use compare: and check the result against NSOrderedAscending, NSOrderedSame and NSOrderedDescending values of NSComparisonResult enum or isEqualToNumber and get the BOOL result directly.

Please refer to docs for details and tips on when to use which method: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDecimalNumber_Class/index.html

siejkowski
  • 1,621
  • 12
  • 11