Nick Forge
Nick Forge

Reputation: 21464

Can I use an NSDecimalNumber anywhere that an NSNumber is expected?

NSDecimalNumber is a subclass of NSNumber, and from what I can tell, it implements all of the NSNumber methods as expected for an NSNumber instance.

Given that, is it ok to give NSDecimalNumbers to any code that is expecting an NSNumber?

The only possible issue might be code that checks that an argument is an instance of NSNumber, but since NSNumber is a class-cluster, code like this would have to check that the instance is a subclass of NSNumber, and NSDecimalNumber instances should pass the same tests.

Upvotes: 3

Views: 1658

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96333

Yes.

The only possible issue might be code that checks that an argument is an instance of NSNumber, but since NSNumber is a class-cluster, code like this would have to check that the instance is a subclass of NSNumber, and NSDecimalNumber instances should pass the same tests.

That's where isKindOfClass: comes in, although you may prefer to test whether the object responds to a message like integerValue instead.

Upvotes: 7

Related Questions