formatString 的奇怪 NSString 行为
我今天遇到了一个非常奇怪的问题。 我有这个静态方法(我刚刚创建的 CommonUtilities 源文件的一部分,它收集了我想在代码中的任何位置访问的所有小型通用方法,就像我通常做的那样......)
我只想使用国际系统符号(k、M、G 等)将数字转换为其科学值,
这是代码:
+ (NSString*)scientificFormatedStringForValue:(NSNumber*)value andUnits:(NSString*)_units
{
NSMutableString* retStr = [NSMutableString string];
long long longValue = [value longLongValue];
if (longValue > 1000000000)
{
[retStr appendFormat:@"%d Md%@", longValue / 1000000000, _units];
}
else if (longValue > 1000000)
{
[retStr appendFormat:@"%d M%@", longValue / 1000000, _units];
}
else if (longValue > 1000)
{
[retStr appendFormat:@"%d k%@", longValue / 1000, _units];
}
else
{
[retStr appendFormat:@"%d %@", longValue, _units];
}
return retStr;
}
这很简单,对吗? 好的,问题是这样的:_units
未正确转换。
在我的示例中,我使用这个:
[CommonUtilities scientificFormatedStringForValue:[NSNumber numberWithLongLong:longValue] andUnits:@"€"];
我将 (null)
作为格式化字符串的 _units
。如果我打印 _units
的值,它是一个点。因此,为了尝试调试这个,我只是将:替换
[retStr appendFormat:@"%d M%@", longValue / 1000000, _units];
为
[retStr appendFormat:@"%d M%@", longValue / 1000000, @"€"];
仍然不起作用。它尝试传递一个字符(认为也许 € 必须转换为一些 UTF8 的东西或其他什么。所以我将调用方法更改为:
[CommonUtilities scientificFormatedStringForValue:[NSNumber numberWithLongLong:longValue] andUnits:@"e"];
仍然是蹩脚的东西。我什至将 @"€" 更改为 [NSString stringWithString:@"€" ],但仍然是相同的输出!我不知道这里出了什么问题,
我想到了源文件的编码问题,所以我删除了它并重新创建了它,但仍然是同样的问题。 ……
如果有的话即使是最微小的线索,那将是非常有帮助的。
I ran into a pretty weird issue today.
I have this static method (part of a CommonUtilities source file that I've been just created that gathers all the small common methods I'd like to access anywhere in my code, like I usually do btw...)
I just want to convert a number into its scientific value using the international system notation (k, M, G, etc.)
Here is the code:
+ (NSString*)scientificFormatedStringForValue:(NSNumber*)value andUnits:(NSString*)_units
{
NSMutableString* retStr = [NSMutableString string];
long long longValue = [value longLongValue];
if (longValue > 1000000000)
{
[retStr appendFormat:@"%d Md%@", longValue / 1000000000, _units];
}
else if (longValue > 1000000)
{
[retStr appendFormat:@"%d M%@", longValue / 1000000, _units];
}
else if (longValue > 1000)
{
[retStr appendFormat:@"%d k%@", longValue / 1000, _units];
}
else
{
[retStr appendFormat:@"%d %@", longValue, _units];
}
return retStr;
}
This is pretty easy right?
Ok, here's the deal: the _units
is not converted properly.
In my example, I use this:
[CommonUtilities scientificFormatedStringForValue:[NSNumber numberWithLongLong:longValue] andUnits:@"€"];
I get (null)
as _units
for the formatted string. If I print the value of _units
, it's a dot. So, to try to debug this, I just replaced:
[retStr appendFormat:@"%d M%@", longValue / 1000000, _units];
with
[retStr appendFormat:@"%d M%@", longValue / 1000000, @"€"];
Still doesn't work. It tried passing a single character (thinking the maybe the € must be converted to some UTF8 stuff or whatever. So I changed the calling method to :
[CommonUtilities scientificFormatedStringForValue:[NSNumber numberWithLongLong:longValue] andUnits:@"e"];
Still crappy stuff. I even changed @"€" to [NSString stringWithString:@"€"], but still the same output! I can't figure out what's wrong here, I'm stuck.
I thought of a problem in the encoding of the source file, so I deleted it and recreated it, but still the same issue....
If any one has even the tiniest clue, that would be most helpful. Thank you guys...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在尝试写入
int
(32 位),但您传递的是long long
(64 位),并且它正在读取int 的前 4 个字节code>longValue
表示value
,最后 4 个字节表示_units
。恰好你的long long
的低字节值为nil
,并没有导致崩溃。您需要使用%lld
而不是%d
才能正确打印 long 值。The problem is you are attempting to write an
int
(32 bits) but you are passing along long
(64 bits) and it is reading the first 4 bytes of yourlongValue
for thevalue
and the last 4 bytes for the_units
. It just so happens that the value in low bytes of yourlong long
isnil
and did not cause a crash. You need to use%lld
instead of%d
to print the long value correctly.