views:

194

answers:

1

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.

+5  A: 

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.

Peter Hosey
In the Core Plot framework, we have two rendering paths, depending on the precision of the supplied input (NSNumbers or NSDecimalNumbers). For that, we use [number isKindOfClass:[NSDecimalNumber class]] to differentiate between the two.
Brad Larson