草庐IT

ios - 当 UITextField 包含货币符号时,NSDecimalNumber 数学运算导致应用程序崩溃

coder 2024-01-23 原文

我正在使用以下代码尝试基于两个文本字段执行简单的数学运算。问题是文本字段中有货币符号,它会导致 NSDecimalNumber 崩溃,如果我的文本字段在字符串中没有货币符号,它可以正常工作。

我尝试添加第二个 currencyFormatter 以将货币设置为空白,但我不知道如何使用它。

- (IBAction)transactionDetailViewQtyTxt_EditDidEnd:(id)sender {
    NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
    [currencyFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    [currencyFormatter setCurrencySymbol:[NWTillHelper getCurrencySymbol]];

    NSNumberFormatter *currencyFormatter2 = [[NSNumberFormatter alloc] init];
    [currencyFormatter2 setNumberStyle: NSNumberFormatterDecimalStyle];
    [currencyFormatter2 setCurrencySymbol:@""];

    _transactionDetailViewSumTxt.text = [currencyFormatter stringFromNumber:[[NSDecimalNumber decimalNumberWithString:_transactionDetailViewPriceTxt.text] decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithString:_transactionDetailViewQtytxt.text]]];
}

下面是崩溃日志

2016-11-30 21:17:37.913 NWMobileTill[2048:52396] *** Terminating app due to uncaught exception 'NSDecimalNumberOverflowException', reason: 'NSDecimalNumber overflow exception'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e26234b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010d8a621e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e2cb265 +[NSException raise:format:] + 197
    3   Foundation                          0x000000010d4d0917 -[NSDecimalNumberHandler exceptionDuringOperation:error:leftOperand:rightOperand:] + 193
    4   Foundation                          0x000000010d4cf761 _checkErrorAndRound + 65
    5   Foundation                          0x000000010d4cfa31 -[NSDecimalNumber decimalNumberByMultiplyingBy:withBehavior:] + 159
    6   NWMobileTill                        0x000000010d28a6d8 -[TransactionDetailView transactionDetailViewQtyTxt_EditDidEnd:] + 440
    7   UIKit                               0x000000010e6875b8 -[UIApplication sendAction:to:from:forEvent:] + 83
    8   UIKit                               0x000000010e80cedd -[UIControl sendAction:to:forEvent:] + 67
    9   UIKit                               0x000000010e80d1f6 -[UIControl _sendActionsForEvents:withEvent:] + 444
    10  UIKit                               0x000000010f19a6f8 -[UITextField _resignFirstResponder] + 313
    11  UIKit                               0x000000010e89d054 -[UIResponder _finishResignFirstResponder] + 286
    12  UIKit                               0x000000010f19a4e6 -[UITextField _finishResignFirstResponder] + 49
    13  UIKit                               0x000000010e89d103 -[UIResponder resignFirstResponder] + 140
    14  UIKit                               0x000000010f19a3b5 -[UITextField resignFirstResponder] + 136
    15  UIKit                               0x000000010e89cd93 -[UIResponder becomeFirstResponder] + 358
    16  UIKit                               0x000000010e736151 -[UIView(Hierarchy) becomeFirstResponder] + 138
    17  UIKit                               0x000000010f199280 -[UITextField becomeFirstResponder] + 51
    18  UIKit                               0x000000010ebd695f -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 206
    19  UIKit                               0x000000010ebda177 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 3823
    20  UIKit                               0x000000010ebc7e41 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
    21  UIKit                               0x000000010ebcfbe0 _UIGestureRecognizerSendTargetActions + 109
    22  UIKit                               0x000000010ebcd6af _UIGestureRecognizerSendActions + 227
    23  UIKit                               0x000000010ebcc93b -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 891
    24  UIKit                               0x000000010ebb8a0e _UIGestureEnvironmentUpdate + 1395
    25  UIKit                               0x000000010ebb8453 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 521
    26  UIKit                               0x000000010ebb7636 -[UIGestureEnvironment _updateGesturesForEvent:window:] + 286
    27  UIKit                               0x000000010e6f63b9 -[UIWindow sendEvent:] + 3989
    28  UIKit                               0x000000010e6a363f -[UIApplication sendEvent:] + 371
    29  UIKit                               0x000000010ee9571d __dispatchPreprocessedEventFromEventQueue + 3248
    30  UIKit                               0x000000010ee8e3c7 __handleEventQueue + 4879
    31  CoreFoundation                      0x000000010e207311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    32  CoreFoundation                      0x000000010e1ec59c __CFRunLoopDoSources0 + 556
    33  CoreFoundation                      0x000000010e1eba86 __CFRunLoopRun + 918
    34  CoreFoundation                      0x000000010e1eb494 CFRunLoopRunSpecific + 420
    35  GraphicsServices                    0x0000000112512a6f GSEventRunModal + 161
    36  UIKit                               0x000000010e685964 UIApplicationMain + 159
    37  NWMobileTill                        0x000000010d28869f main + 111
    38  libdyld.dylib                       0x000000011042d68d start + 1
    39  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

为什么不先去掉货币符号呢?

NSString * numberNoCurrency = [_transactionDetailViewPriceTxt.text stringByReplacingOccurrencesOfString:@"$" withString:@""];

然后将该字符串提供给您的十进制转换以进行乘法运算。

关于ios - 当 UITextField 包含货币符号时,NSDecimalNumber 数学运算导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40890111/

有关ios - 当 UITextField 包含货币符号时,NSDecimalNumber 数学运算导致应用程序崩溃的更多相关文章

  1. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  2. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  3. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  4. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  5. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  6. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  7. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  8. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  9. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  10. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

随机推荐