Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top