1

My iOS app terminates in Xcode simulator due to EXC_BAD_ACCESS error on following line

if ([amount.text isEqualToString:@""] || [chargeAmount floatValue] == 0.0) {

The complete block of code is

if ([amount.text isEqualToString:@""] || [chargeAmount floatValue] == 0.0) {
    // Focus on the poptip target.
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [transactionView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    [self performSelector:@selector(amountRequiredPopup) withObject:nil afterDelay:0.3];
    return;
}

I check amount.text in NSLog and it is showing the value but not the ChargeAmount. It seems there is bad value of chargeAmount. chargeAmount is defined in interface in .h file as follows:

NSDecimalNumber* chargeAmount;

I am new in objective C and iOS app development. Please suggest where is the issue? Thank you in advance.

Yas
  • 71
  • 1
  • 10
  • Where is `chargeAmount` initialized? – vadian Jul 31 '17 at 04:44
  • @vadian in the .h file as below: `@interface TransactionFormViewController : GAITrackedViewController { IBOutlet UITextField *amount; IBOutlet UITextField *discount; NSDecimalNumber* chargeAmount; // defined here NSDecimalNumber* discountAmount; NSDecimalNumber* totalChargeAmount; InputType inputType; }` – Yas Jul 31 '17 at 04:47
  • @vadian its wierd because you can call [chargeAmount floatValue] even if chargeAmount is nil and this not crash the app, tested with `NSDecimalNumber* chargeAmount;` `NSLog(@"%f",[chargeAmount floatValue]);` this no crash at all – Reinier Melian Jul 31 '17 at 04:48
  • In the .h file it's **defined**. I'm asking where it's **initialized** (assigning a value to it). And which exception reason do you get? – vadian Jul 31 '17 at 04:48
  • I think that the problem is `[amount.text isEqualToString:@""]`can you check if `amount.text`is nil? please put an NSLog(@"%@",amount.text) and post what prints the console – Reinier Melian Jul 31 '17 at 04:49
  • @vadian I am not sure about this, I couldn't see any where in both files about initialization. – Yas Jul 31 '17 at 04:51
  • @ReinierMelian thanks for the reply. Let me check that please. – Yas Jul 31 '17 at 04:52
  • @ReinierMelian I checked `amount.text` and it is showing the value, the problem is in `chargeAmount`. But I am not sure what is the issue actually. – Yas Jul 31 '17 at 04:55
  • must be another kind of issue because I tested even if chargeAmount is nil this don´t crash the app, maybe you are putting there another type, can you please post all your crash log message? – Reinier Melian Jul 31 '17 at 04:57
  • I am not sure about crash log, I can see following message in the console. `2017-07-31 13:00:59.418170 SwipeHQC[69083:3405474] [] __nwlog_err_simulate_crash simulate crash already simulated "nw_socket_set_common_sockopts setsockopt SO_NOAPNFALLBK failed: [42] Protocol not available"` – Yas Jul 31 '17 at 05:05
  • first add a IOS enviroment variable to avoid those logs, go to your app scheme and go to edit scheme and add this in a section called Eviroment Variables add this `OS_ACTIVITY_MODE`and in value put Disable, let me know when you have done this – Reinier Melian Jul 31 '17 at 05:10
  • Yes I did this now. – Yas Jul 31 '17 at 05:21
  • @yas then try to get the message of your crash must be EXC_BAD_ACCESS and something else – Reinier Melian Jul 31 '17 at 05:24
  • Now the last message in the console is as follow: `2017-07-31 13:23:22.751 App[69974:3423386] Can't find keyplane that supports type 8 for keyboard iPhone-Portrait-DecimalPad; using 309711205_Portrait_iPhone-Simple-Pad_Default` – Yas Jul 31 '17 at 05:24
  • is hard to explain without images, add an exception breakpoint, do you know how do it? – Reinier Melian Jul 31 '17 at 05:27
  • I don't know that but I will check that. Should I add that just above that line of code? – Yas Jul 31 '17 at 05:28
  • @Yas check this https://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode when you have done this let me know – Reinier Melian Jul 31 '17 at 05:31
  • @ReinierMelian I added exception break point and now, it is showing the following error: `[NSMutableRLEArray floatValue]: unrecognized selector sent to instance 0x7ba141f0` – Yas Jul 31 '17 at 07:07
  • @Yas my answer finally help you? can you accept my answer? thanks – Reinier Melian Aug 02 '17 at 19:28

2 Answers2

0

I would suggest instead of NSDecimalNumber* chargeAmount; you can define that variable as float and set its value to 0.0 in viewDidLoad() and then later you can set it to as per your code and add that check on it.

Paras Gorasiya
  • 1,295
  • 2
  • 13
  • 33
0

Based on your last comment, as i think the issue is that you have assigned a value of another type into your chargeAmount variable, search all your code related to chargeAmount and post there, in some part of your code you are assigning a value of type NSMutableAttributedString to your chargeAmount variable and then when you call [chargeAmount floatValue] crash because NSMutableAttributedString don't have any method called floatValue

Hope this helps you

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • chargeAmount is defined in .h file as `NSDecimalNumber* chargeAmount;` and then being used in .m file as `if ([amount.text isEqualToString:@""] || [chargeAmount floatValue] == 0.0) {`, at same point it is being crashed. I thing there should be some thing else than `floatValue` but I am not sure what should be, please suggest. – Yas Aug 03 '17 at 03:40
  • @Yas check the class of your chargeAmount before the crash, something like ` NSLog(@"%@",[chargeAmount class]);` and let me know what prints – Reinier Melian Aug 03 '17 at 04:17
  • I did this and it is crashing on this line ` NSLog(@"%@",[chargeAmount class]);` as well. But just to check if I assign a value 0, `chargeAmount = 0;` and then it is displaying `null` for class and not crashing as well. – Yas Aug 03 '17 at 04:32
  • 1
    I added the line of code `chargeAmount = [NSDecimalNumber decimalNumberWithString:amount.text];` above that block and it solved the issue. Actually chargeAmount was not given any value so it was crashing at float conversion. Thank you for your help and answer. – Yas Aug 03 '17 at 08:28
  • @Yas I am glad to help you, and hear that finally the issue is solved – Reinier Melian Aug 03 '17 at 12:38