0

This time I am having problems with not being able to store some data in an NSMutablearray.

The process is like this:

I click on a button that loads a UIPopOver and I make the selection of a product and its amount, through UIPIckerView, when I click the Done button, the amount, product and total price appears on a UITextView. I'm ok with that.

The problem is that I need to store each selection from the UIPickerView in the NSMutablearray for a later calculation of the sum of all products selected, but it is not storing in the NSMutablerarray.

So, anyone could help???

-(void)donePressed{



//Cálculo do valor a pagar
float quantFlt = [[arrayQtdProdutos objectAtIndex:[categoryPicker selectedRowInComponent:1]] floatValue];
float valorFlt = [[valorArray objectAtIndex:[categoryPicker selectedRowInComponent:0]] floatValue];
float total = quantFlt * valorFlt;

_msg = [NSString stringWithFormat: @"%@ - %@ - R$ %.2f",
        [arrayQtdProdutos objectAtIndex:[categoryPicker selectedRowInComponent:1]],
        [arrayOfCategories objectAtIndex:[categoryPicker selectedRowInComponent:0]],
        total];

_txtViewProdutos.text = [NSString stringWithFormat:@"%@ \n%@", _txtViewProdutos.text, _msg];

[arrayProdutosComprados addObject:_msg];
NSLog(@"%i", arrayProdutosComprados.count);

[popOverController dismissPopoverAnimated:YES];
}
12
  • 2
    What is msg? Is it supposed to be a property? Commented Nov 14, 2013 at 16:52
  • 'msg'is a NSString that gets the amount, product and price of the selected row. And then is shown at the UITextView. Commented Nov 14, 2013 at 16:56
  • If you set a break point after you set msg I suspect that its going to be nil. So again, is it supposed to be a property? Commented Nov 14, 2013 at 16:56
  • Aparently yes, when I NSLog the NSMutablearray count it gets me '0'. As to whether should be a property or no, I think no. Commented Nov 14, 2013 at 17:01
  • You should add a property @property (strong, nonatomic) NSString *msg then change msg = to self.msg = And when you want to use it call self.msg Commented Nov 14, 2013 at 17:04

2 Answers 2

1

This is the correct code with the help of @SPA.

-(void)donePressed{

//Cálculo do valor a pagar
float quantFlt = [[arrayQtdProdutos objectAtIndex:[categoryPicker selectedRowInComponent:1]] floatValue];
float valorFlt = [[valorArray objectAtIndex:[categoryPicker selectedRowInComponent:0]] floatValue];
float total = quantFlt * valorFlt;

_msg = [NSString stringWithFormat: @"%@ - %@ - R$ %.2f",
    [arrayQtdProdutos objectAtIndex:[categoryPicker selectedRowInComponent:1]],
    [arrayOfCategories objectAtIndex:[categoryPicker selectedRowInComponent:0]],
    total];

_txtViewProdutos.text = [NSString stringWithFormat:@"%@ \n%@", _txtViewProdutos.text, _msg];

[arrayProdutosComprados addObject:_msg];
NSLog(@"%i", arrayProdutosComprados.count);

[popOverController dismissPopoverAnimated:YES];
}


- (void)viewDidLoad
{
[super viewDidLoad];

arrayProdutosComprados = [NSMutableArray new];
}
Sign up to request clarification or add additional context in comments.

Comments

0

I agree with SPA, it is more than likely that arrayProdutosComprados is nil.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.