Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-11-2012, 08:50 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 15
Killbill is on a distinguished road
Default Pick random number from array + running total

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!

In the .h:
Code:
IBOutlet UILabel *anumber;
IBOutlet UILabel *bnumber;
IBOutlet UILabel *cnumber;
IBOutlet UILabel *dnumber;
NSMutableArray *randomB;
NSMutableArray *numberPicker;
    
}



-(IBAction)Calculatenow;


In the .m:

Code:
float VarA = 0.00;
float VarB = 0.00;
float VarC = 0.00;
float VarD = 0.00;
float result = 0.00;


@implementation DataViewController

@synthesize dataObject = _dataObject;





-(IBAction) Calculatenow{
    result = [[numberPicker objectAtIndex:arc4random() % [numberPicker count]] floatValue];
    
    VarA = VarC;
    VarB = result;
    VarC = VarA + VarB;
   
    
    anumber.text = [[NSNumber numberWithFloat:VarA] stringValue];
    bnumber.text = [[NSNumber numberWithFloat:VarB] stringValue];
    cnumber.text = [[NSNumber numberWithFloat:VarC] stringValue];
    dnumber.text = [[NSNumber numberWithFloat:VarD] stringValue];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    anumber.text = @"0.00";
    bnumber.text = @"0,00";
    cnumber.text = @"0.00";
    dnumber.text = @"0.00";
    
    
    
    
    
    [super viewDidLoad];
    
    NSMutableArray *numberPicker = [NSArray arrayWithObjects:
                                    [NSNumber numberWithFloat:10.00],
                                    [NSNumber numberWithFloat:2.56],
                                    [NSNumber numberWithFloat:4.25],
                                    [NSNumber numberWithFloat:1.95],
                                    nil];

    
    
   
	
}

Last edited by Killbill; 01-11-2012 at 08:58 AM.
Killbill is offline   Reply With Quote
Old 01-11-2012, 09:22 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Killbill View Post
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!

In the .h:
Code:
IBOutlet UILabel *anumber;
IBOutlet UILabel *bnumber;
IBOutlet UILabel *cnumber;
IBOutlet UILabel *dnumber;
NSMutableArray *randomB;
NSMutableArray *numberPicker;
    
}



-(IBAction)Calculatenow;


In the .m:

Code:
float VarA = 0.00;
float VarB = 0.00;
float VarC = 0.00;
float VarD = 0.00;
float result = 0.00;


@implementation DataViewController

@synthesize dataObject = _dataObject;





-(IBAction) Calculatenow{
    result = [[numberPicker objectAtIndex:arc4random() % [numberPicker count]] floatValue];
    
    VarA = VarC;
    VarB = result;
    VarC = VarA + VarB;
   
    
    anumber.text = [[NSNumber numberWithFloat:VarA] stringValue];
    bnumber.text = [[NSNumber numberWithFloat:VarB] stringValue];
    cnumber.text = [[NSNumber numberWithFloat:VarC] stringValue];
    dnumber.text = [[NSNumber numberWithFloat:VarD] stringValue];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    anumber.text = @"0.00";
    bnumber.text = @"0,00";
    cnumber.text = @"0.00";
    dnumber.text = @"0.00";
    
    
    
    
    
    [super viewDidLoad];
    
    NSMutableArray *numberPicker = [NSArray arrayWithObjects:
                                    [NSNumber numberWithFloat:10.00],
                                    [NSNumber numberWithFloat:2.56],
                                    [NSNumber numberWithFloat:4.25],
                                    [NSNumber numberWithFloat:1.95],
                                    nil];

    
    
   
	
}


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):

Code:
@property (nonatomic, retain) NSMutableArray *numberPicker;
or, for ARC applications:

Code:
@property (nonatomic, strong) NSMutableArray *numberPicker;
You need to change that line to assign a value to your property instead of creating a local variable.

Code:
  self.numberPicker = [NSArray arrayWithObjects:
    [NSNumber numberWithFloat:10.00],
    [NSNumber numberWithFloat:2.56],
    [NSNumber numberWithFloat:4.25],
    [NSNumber numberWithFloat:1.95],
    nil];

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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-11-2012, 10:02 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 15
Killbill is on a distinguished road
Default

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.
Killbill is offline   Reply With Quote
Old 01-11-2012, 10:25 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Killbill View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-11-2012, 10:56 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 15
Killbill is on a distinguished road
Default

Thank you for your patience!

In the .h
Code:
@interface DataViewController : UIViewController{


IBOutlet UILabel *anumber;
IBOutlet UILabel *bnumber;
IBOutlet UILabel *cnumber;
IBOutlet UILabel *dnumber;
NSMutableArray *numberPicker;
    
}



-(IBAction)Calculatenow;


@property (strong, nonatomic) id dataObject;
@property (nonatomic, retain) NSMutableArray *numberPicker;
@end
In the .m

Code:
@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
Killbill is offline   Reply With Quote
Old 01-11-2012, 01:03 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Killbill View Post
Thank you for your patience!

In the .h
Code:
@interface DataViewController : UIViewController{


IBOutlet UILabel *anumber;
IBOutlet UILabel *bnumber;
IBOutlet UILabel *cnumber;
IBOutlet UILabel *dnumber;
NSMutableArray *numberPicker;
    
}



-(IBAction)Calculatenow;


@property (strong, nonatomic) id dataObject;
@property (nonatomic, retain) NSMutableArray *numberPicker;
@end
In the .m

Code:
@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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-12-2012, 06:55 AM   #7 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 15
Killbill is on a distinguished road
Default

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

Thanks
Killbill is offline   Reply With Quote
Reply

Bookmarks

Tags
array, nsmutable array, nsnumber

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 386
10 members and 376 guests
7twenty7, Atatator, buggen, guusleijsten, j.b.rajesh@gmail.com, QuantumDoja, sacha1996, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:38 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0