尝试从 NSString 中删除小数
这很奇怪。
为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在某些时候,您将
NSDecimalNumber
分配给distanceFromTargetString
而不是NSString
。 Objective C 中没有对赋值进行运行时类型检查,因此这完全是“合法的”:在您尝试将
NSString
方法发送到string< 之前,上述内容不会生成任何错误或警告。 /code>,此时您将收到如上所示的异常(崩溃)。
对分配
distanceFromTargetString
的所有位置以及使用NSDecimalNumber
的所有位置进行审核。在某个地方,你正在渡过溪流。At some point you are assigning an
NSDecimalNumber
todistanceFromTargetString
rather than anNSString
. There is no run-time type checking of assignments in Objective C, so this is totally "legal":The above will generate no errors or warnings until you try to send
NSString
methods tostring
, at which point you will get an exception (crash) like you show above.Do an audit of everywhere you assign
distanceFromTargetString
, and everywhere you useNSDecimalNumber
. Somewhere you're crossing the streams.你可以尝试:
You could try :
您正在某处调用
isEqualToString
并使用NSDecimalNumber
作为接收者。什么是distanceFromTarget
?这是一个NSDecimalNumber
吗?第一件事应该有效。
You are somewhere calling
isEqualToString
with aNSDecimalNumber
as the receiver. What isdistanceFromTarget
? Is this anNSDecimalNumber
?The first thing should work.
您可以尝试在该行设置一个断点
,看看 distanceFromTargetString 是否实际上是一个
NSString
。如上所述,
NSNumber
不知何故潜入了某个地方。You could try to set a breakpoint at the line
and see if distanceFromTargetString is actually an
NSString
.As mentioned above, somehow an
NSNumber
has sneaked in somewhere.