NSNumberFormatter 导致 iPhone SDK 4.0b2 崩溃

发布于 2024-08-30 12:22:25 字数 649 浏览 9 评论 0原文

我有一个应用程序,它已经在应用程序商店中存在了一段时间,并且在操作系统 3.1 - 3.13 上运行良好。然而,当在 4.0b2 上测试时,我注意到它每次都会在同一个地方崩溃,但只是在设备上,从来没有在模拟器上。我用的是3GS来测试。

在 loadView 上,我初始化了一个 NSNumberFormatter 对象,该对象在界面中声明并保留,因此我可以在任何地方访问它。在我的方法中,我多次调用它来将字符串值转换为 nsnumbers 以存储在可变字典中。

这是一个示例:

[myDictionary setObject:[myStyleFormatter numberFromString:@"1"] forKey:@"hours"];
[myDictionary setObject:[myStyleFormatter numberFromString:@"30"] forKey:@"minutes"];
[myDictionary setObject:[myStyleFormatter numberFromString:@"10"] forKey:@"seconds"];

由于某种原因,它在尝试设置时间时立即崩溃。错误是“尝试插入零值(键:小时)”

我一直在做错什么吗? 4.0b2 的 api 有变化吗?

谢谢,

豪伊

I've got an app that's been in the app store for a while and functions perfectly on OS 3.1 - 3.13. However, when tested on 4.0b2 I noticed that it crashes in the same place every time, but only on the device, never on the simulator. I'm using a 3GS to test.

On loadView I initialize an NSNumberFormatter object which is declared and retained in the interface so I have access to it everywhere. In my method I call it several times to convert string values into nsnumbers to be stored in a mutable dictionary.

Here's an example:

[myDictionary setObject:[myStyleFormatter numberFromString:@"1"] forKey:@"hours"];
[myDictionary setObject:[myStyleFormatter numberFromString:@"30"] forKey:@"minutes"];
[myDictionary setObject:[myStyleFormatter numberFromString:@"10"] forKey:@"seconds"];

For some reason it crashes as soon as it tries to set hours. The error is "attempt to insert nil value (key: hours)"

Have I been doing something wrong all along? Has the api changed for 4.0b2?

Thanks,

Howie

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半寸时光 2024-09-06 12:22:25

我也有同样的问题。我追踪到一个 NSNumberFormatter 语句,该语句不喜欢数字中每 3 位数字有一个空格(或逗号)。这是使用数字格式化程序的原因之一。

NSNumber *number = [currencyFormatter numberFromString:mstring];

在互联网上的许多示例中,这是相当标准的代码,因此我怀疑很多人都会发现问题。

无论如何,我通过删除空格来修复它

NSArray *sArray = [mstring componentsSeparatedByString:@" "];
[mstring setString:@" "]; //space at beginning is OK, would prefer nil
for (NSString *sElement in sArray) {
    [mstring appendString:sElement];
}

然后 currencyFormatter 行起作用了。

但是,在我的代码的另一个区域中,相同的 currencyFormatter 语句没有任何问题。我尝试更改该区域的代码来导致问题,但我做不到。

所以,非常好奇!!!

I had the same problem. I tracked it down to a NSNumberFormatter statement that did not like spaces (or commas) every 3 digits in the numbers. Which is one of the reasons for having a number formatter.

NSNumber *number = [currencyFormatter numberFromString:mstring];

It is fairly standard code in many examples on the internet, so I suspect a lot will find the problem.

Anyway, I fixed it by getting rid of the spaces

NSArray *sArray = [mstring componentsSeparatedByString:@" "];
[mstring setString:@" "]; //space at beginning is OK, would prefer nil
for (NSString *sElement in sArray) {
    [mstring appendString:sElement];
}

Then the currencyFormatter line worked.

BUT, in another area of my code, that same currencyFormatter statement worked with no problem. I tried changing code in the area to cause the problem, but I could not.

So, very curious!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文