formatString 的奇怪 NSString 行为

发布于 2024-12-21 02:56:48 字数 1719 浏览 1 评论 0原文

我今天遇到了一个非常奇怪的问题。 我有这个静态方法(我刚刚创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-12-28 02:56:48

问题是您正在尝试写入 int (32 位),但您传递的是 long long (64 位),并且它正在读取 int 的前 4 个字节code>longValue 表示 value,最后 4 个字节表示 _units。恰好你的long long的低字节值为nil,并没有导致崩溃。您需要使用 %lld 而不是 %d 才能正确打印 long 值。

    longValue       NSString*
/---------------\ /---------\
|   8 bytes     | | 4 bytes |
\---------------/ \---------/
^^^^^^^^ ^^^^^^^^  ^^^^^^^^^
   %d       %@        This gets ignored.
(reads 4) (reads this 4 bytes which happen to be nil)

The problem is you are attempting to write an int (32 bits) but you are passing a long long (64 bits) and it is reading the first 4 bytes of your longValue for the value and the last 4 bytes for the _units. It just so happens that the value in low bytes of your long long is nil and did not cause a crash. You need to use %lld instead of %d to print the long value correctly.

    longValue       NSString*
/---------------\ /---------\
|   8 bytes     | | 4 bytes |
\---------------/ \---------/
^^^^^^^^ ^^^^^^^^  ^^^^^^^^^
   %d       %@        This gets ignored.
(reads 4) (reads this 4 bytes which happen to be nil)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文