Objective-C – Can I use nsdecimalnumber anywhere in the expected NSNumber?

NSDecimalNumber is a subclass of NSNumber. As far as I know, it implements all NSNumber methods expected by NSNumber instances.

In view of this, is it possible? Provide NSDecimalNumbers to any code that expects NSNumber?

The only possible problem may be that the check parameter is the code of an NSNumber instance, but since NSNumber is a class cluster, such code must check that the instance is a subclass of NSNumber, and the NSDecimalNumber instance should pass its test.

p>

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.

This is isKindOfClass: here, although you may prefer to test whether the object responds to messages like integerValue.

NSDecimalNumber is a subclass of NSNumber. As far as I know, it implements all the NSNumber methods expected by NSNumber instances.

In view of this, is it possible to provide NSDecimalNumbers to any code that expects NSNumber?

The only possible problem may be that the check parameter is the code of an NSNumber instance, but since NSNumber is a class cluster, such code must check that the instance is a subclass of NSNumber, and the NSDecimalNumber instance should pass its test.

p>

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.

This is the isKindOfClass: here, although you may prefer to test whether the object responds to a message like integerValue.

Leave a Comment

Your email address will not be published.