简体   繁体   English

我可以在需要NSNumber的任何地方使用NSDecimalNumber吗?

[英]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. NSDecimalNumber是NSNumber的子类,据我所知,它实现了对NSNumber实例所期望的所有NSNumber方法。

Given that, is it ok to give NSDecimalNumbers to any code that is expecting an NSNumber? 鉴于此,是否可以将NSDecimalNumbers赋予期望使用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. 唯一可能的问题可能是检查参数是否为NSNumber实例的代码,但由于NSNumber是类群集,因此类似的代码将必须检查该实例是否为NSNumber的子类 ,并且NSDecimalNumber实例应传递相同的代码。测试。

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. 唯一可能的问题可能是检查参数是否为NSNumber实例的代码,但是由于NSNumber是类群集,因此类似的代码将必须检查该实例是否为NSNumber的子类,并且NSDecimalNumber实例应传递相同的代码。测试。

That's where isKindOfClass: comes in, although you may prefer to test whether the object responds to a message like integerValue instead. 那就是isKindOfClass:来源,尽管您可能更愿意测试对象是否响应诸如integerValue类的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM