Hi,
I'm stuck....
I'm trying to make an app which upon button click picks random number from an array. Upon next button click it add up the values (running total of picked numbers).
I get an unused varibale 'numberPicker' and EXC_ARITHMETIC error and i don't understand why.
Help is much apprecriated!
Hi,
I'm stuck....
I'm trying to make an app which upon button click picks random number from an array. Upon next button click it add up the values (running total of picked numbers).
I get an unused varibale 'numberPicker' and EXC_ARITHMETIC error and i don't understand why.
Help is much apprecriated!
You need to read up on variable scope, and on Cocoa memory management.
You've defined an instance variable numberPicker. Then in your viewDidLoad method, you create a local variable which is also called numberPicker. That local variable gets forgotten when you exit your viewDidLoad method (the local variable "goes out of scope".)
You should change your instance variable numberPicker to a retained property (or a strong property, if you're using ARC):
Then you need to set the numberPicker property to nil in your viewDidUnload method:
Code:
self.numberPicker = nil;
There may be other problems with your code as well. Like I said, you need to stop and learn about memory management and the fundamental differences between local variables, instance variables, and properties, before you do anything else. Until you understand those concepts, you are going to be lost, and your code will crash all over the place.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Thank you. I got it working.
I think you right on the memory issue. It doesn't always produce
a result... maybe 3 out of 10 button clicks.
Does this have to do with the arc4random? Does that make it sluggish?
Thank you for your response.
Thank you. I got it working.
I think you right on the memory issue. It doesn't always produce
a result... maybe 3 out of 10 button clicks.
Does this have to do with the arc4random? Does that make it sluggish?
Thank you for your response.
The arc4Random method will return thousands of results a second. If you're using it as part of the calculation on every pixel on the screen, it will make things slow. If you're calling it a couple of dozen times in response to a user click, you would not see any visible delay.
Post your code. It sounds like you still have problems with it.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
@synthesize dataObject = _dataObject;
@synthesize numberPicker;
-(IBAction) Calculatenow{
result = [[numberPicker objectAtIndex:arc4random() % [numberPicker count]] floatValue];
VarB = result;
VarC = VarA + VarB;
VarA = VarC;
anumber.text = [[NSNumber numberWithFloat:VarA] stringValue];
bnumber.text = [[NSNumber numberWithFloat:VarB] stringValue];
cnumber.text = [[NSNumber numberWithDouble:VarC] stringValue];
dnumber.text = [[NSNumber numberWithDouble:VarD] stringValue];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
self.numberPicker = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:10.01],
[NSNumber numberWithFloat:2.56],
[NSNumber numberWithFloat:4.25],
[NSNumber numberWithFloat:1.95],
nil];
anumber.text = @"0.00";
bnumber.text = @"0.00";
cnumber.text = @"0.00";
dnumber.text = @"0.00";
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
self.numberPicker = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
It seems like that code should work. I would suggest adding a breakpoint at the beginning of your IBAction and stepping through your code to figure out what's going on.
If you don't know how to step through your code in the debugger, you should learn. In the meantime, add log statements that display the new random value as well as the values of the variables that you are displaying.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Hi,
Spent some hours trying to find the problem without any success...
In simulator, after clicking the button 5 times without any result, the sixth time it produces six results. On my device it doesn't produce anything.
I uploaded the project here in case you want to have a look. http://wtrns.fr/hdRAAah6Iqc_ag