为什么NumberFormatter函数func字符串(来自数字:NSNumber) – >串?返回一个字符串?而不是一个字符串? NumberFormatter是否有特定的输入或状态,此函数可以返回nil?有时我听说这些不太理想的返回类型来自Objective-C保持文件,这是其中一种情况吗?
解决方法
The strange behavior isn’t NSNumber — NSNumber -stringValue seems to be returning results one would expect. It’s NSNumberFormatter which is returning unusual results.
The abstract problem here is that NSNumberFormatter was originally written to handle money amounts for EOF,so it doesn’t deal with unusual numbers well (or general purpose string formatting for that matter).
The more concrete problem is that when you ask an NSNumberFormatter for -stringForObjectValue:,if the object is not an NSDecimalNumber,it converts it to an NSDecimalNumber using
[NSDecimalNumber decimalNumberWithString:[objectValue stringValue]]
which is not going to handle unusual values well,in addition to potentially other issues. A further issue,though I don’t know this is the case,is that I don’t think there is an NSDecimal (the struct) representation of positive and negative infinity,as there is for NaN.
我实际上并不理解这种反应足以说明这是为什么返回一个可选项,但我怀疑它是相关的.
原始问题中有示例代码,但没有一个返回nil,它总是给出一个字符串.