尝试从 NSString 中删除小数

发布于 2024-11-25 17:24:38 字数 859 浏览 1 评论 0原文

这很奇怪。

为 iPhone 进行开发 我有一个 NSString:

 distanceFromTargetString = @"6178266.000000"
 // this is not actually code, but a value taken from elsewhere. It is a proper NSString though. 

当我使用这个

NSArray *listItems = [distanceFromTargetString componentsSeparatedByString:@"."];
distanceFromTargetString = [listItems objectAtIndex:0];

或这个时,

 [distanceFromTarget setText: distanceFromTargetString];

我得到类似这样的东西

-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x1cac40
2011-07-21 14:29:24.226 AssassinBeta[7230:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x1cac40'

有什么想法吗?

This is bizarre.

Developing for the iPhone I have an NSString:

 distanceFromTargetString = @"6178266.000000"
 // this is not actually code, but a value taken from elsewhere. It is a proper NSString though. 

When I use this

NSArray *listItems = [distanceFromTargetString componentsSeparatedByString:@"."];
distanceFromTargetString = [listItems objectAtIndex:0];

or this

 [distanceFromTarget setText: distanceFromTargetString];

I get something like this

-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x1cac40
2011-07-21 14:29:24.226 AssassinBeta[7230:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0x1cac40'

Any ideas?

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

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

发布评论

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

评论(4

甜嗑 2024-12-02 17:24:38

在某些时候,您将 NSDecimalNumber 分配给 distanceFromTargetString 而不是 NSString。 Objective C 中没有对赋值进行运行时类型检查,因此这完全是“合法的”:

NSDecimalNumber *number = [NSDecimalNumber ....];
[array addObject:number];
NSString *string = [array lastObject];

在您尝试将 NSString 方法发送到 string< 之前,上述内容不会生成任何错误或警告。 /code>,此时您将收到如上所示的异常(崩溃)。

对分配 distanceFromTargetString 的所有位置以及使用 NSDecimalNumber 的所有位置进行审核。在某个地方,你正在渡过溪流。

At some point you are assigning an NSDecimalNumber to distanceFromTargetString rather than an NSString. There is no run-time type checking of assignments in Objective C, so this is totally "legal":

NSDecimalNumber *number = [NSDecimalNumber ....];
[array addObject:number];
NSString *string = [array lastObject];

The above will generate no errors or warnings until you try to send NSString methods to string, at which point you will get an exception (crash) like you show above.

Do an audit of everywhere you assign distanceFromTargetString, and everywhere you use NSDecimalNumber. Somewhere you're crossing the streams.

茶底世界 2024-12-02 17:24:38

你可以尝试:

NSInteger i = [distanceFromTargetString integerValue];
NSString s = [NSString stringWithFormat:@"%d", i];
// you got your string

You could try :

NSInteger i = [distanceFromTargetString integerValue];
NSString s = [NSString stringWithFormat:@"%d", i];
// you got your string
能否归途做我良人 2024-12-02 17:24:38

您正在某处调用 isEqualToString 并使用 NSDecimalNumber 作为接收者。什么是 distanceFromTarget?这是一个 NSDecimalNumber 吗?

第一件事应该有效。

You are somewhere calling isEqualToString with a NSDecimalNumber as the receiver. What is distanceFromTarget? Is this an NSDecimalNumber?

The first thing should work.

南风起 2024-12-02 17:24:38

您可以尝试在该行设置一个断点

[distanceFromTarget setText: distanceFromTargetString];

,看看 distanceFromTargetString 是否实际上是一个 NSString

如上所述,NSNumber 不知何故潜入了某个地方。

You could try to set a breakpoint at the line

[distanceFromTarget setText: distanceFromTargetString];

and see if distanceFromTargetString is actually an NSString.

As mentioned above, somehow an NSNumber has sneaked in somewhere.

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