2016-11-30 44 views
2

我正在使用以下代碼嘗試基於兩個文本字段執行簡單的數學運算。問題在於文本字段中包含貨幣符號,導致NSDecimalNumber崩潰,如果我在文本字段中沒有貨幣符號,它可以正常工作。當UITextField包含貨幣符號時,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 
+0

請確保您使用的是有效數字,數字不爲零。通過代碼放置一個斷點和跟蹤,以便在執行操作之前可以看到數字。還要確保它是一個有效的數字(即不是零)。 – xdeleon

回答

1

爲什麼不直接去掉貨幣符號第一?

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

然後將該字符串輸入到乘法的十進制轉換中。

+0

如果需要,還有其他一些變體允許您排除一組貨幣符號。 –

0

你可能不想假定一個$符號,但想要除去數字和小數(可能是某些地區的逗號)以外的所有東西。

NSMutableCharacterSet* numbers = [NSMutableCharacterSet decimalDigitCharacterSet]; 
[numbers addCharactersInString:[[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator]]; 
NSCharacterSet* trim = [numbers invertedSet]; 

NSString* result = [[string componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:@""]; 
+0

是的,我有一個幫手來處理貨幣符號是爲了避免做出錯誤的假設 –

+0

請記住,貨幣符號可能會出現在某些地區的數字之後。 – Trygve

+0

注意到謝謝你會記住這一點 –