0

I am trying to store user input data in a text field to an NSNumber variable of an object. Based on my logs, the value stores just fine and everything.

The problem is when the user changes views through navigation, then returns. When I try to access the NSNumber value, I get a bad access error. The odd part is if the value I'm storing is only 1 digit (0-9) i get no crash.

Some code...

   //conversion then storage of the NSNumber value into the object
   NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
   [formatter setNumberStyle:NSNumberFormatterNoStyle];
   NSNumber *num = [formatter numberFromString:textField.text];
   _options.unitsPerCase = num;
   NSLog(@"num: %@",num);
   break; 


   //user leaves screen i reload tabledata and try to 
   //populate with previously saved NSNumber
   case NumberTextFieldSection:
    {
        switch (indexPath.row)
        {
            case NumberTextFieldRow:
            {
                cell.valueTextField.text = [_options.unitsPerCase stringValue];
                break;
            }

            default:
                break;
        }
    }
JMD
  • 1,540
  • 2
  • 24
  • 56
  • I am not sure but try using `self.options` instead of `_options`. That will make your accessors work and it will be retained. – Anoop Vaidya Mar 06 '13 at 18:43
  • have you tried above? – Anoop Vaidya Mar 06 '13 at 18:51
  • isnt self.options and _options the same if you @synthesize the property via `@synthesize options = _options;` ? keep in mind storing values in other variables of the options class work just fine. Its only this NSNumber that is causing error. – JMD Mar 06 '13 at 18:52
  • just now i answered, check and you will know the difference http://stackoverflow.com/questions/15255226/underscore-before-property-name-in-setter/15255266#15255266 – Anoop Vaidya Mar 06 '13 at 18:53
  • self.options did not fix the issue. it still crashes – JMD Mar 06 '13 at 18:56
  • then go for zombie checking – Anoop Vaidya Mar 06 '13 at 18:57
  • What am I looking for with zombie checking? the object is still there, I can pull out other variables from it. Its only this one NSNumber variable that is causing issue. – JMD Mar 06 '13 at 18:58
  • then try replacing the nsnumber by unboxed nsinteger or double or even string. – Anoop Vaidya Mar 06 '13 at 18:59
  • Yes, i could just change the type but that kind of defeats the point of the original question – JMD Mar 06 '13 at 19:09
  • first make it working then try to cover the loose points in your code :) – Anoop Vaidya Mar 06 '13 at 19:09
  • I created a new property of NSInteger and it works fine. But I can't use that because of sections other code use this same property, and core data expects an NSNumber – JMD Mar 06 '13 at 19:11
  • @JMD: How are you transition between view's? Are you making new controller objects each time? – Rakesh Mar 06 '13 at 19:14
  • i simply push the new view onto the view controller. the user makes a selection at the next screen, then i send that selection back via delegate and update the corresponding _options property. then refresh tabledata to display the new selection. all other properties work except the NSNumber – JMD Mar 06 '13 at 19:20

1 Answers1

3

I suspect unitsPerCase property check it if is declared strong or retained in the myOptions class.

use

@property (nonatomic, strong) NSNumber *unitsPerCase;
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101