I am trying to use arc4random but it os causing my app to crash. On my view I have a text field and the user will enter a number, when the hit a button this number is used for the range, the code I use is as follows:
int myInt1 = [textfield.text intValue];
int fromNumber = 1;
int rnumber = (arc4random() % (myInt1 - fromNumber)) + fromNumber;
number1.text = [[NSString alloc] initWithFormat:@"%i",rnumber];
It will work if I use 50 for example instead of myInt1 but I need the user input. Any help is greatly appreciated.
I have noticed that it will work when there is a figure in the textfield, when this is left blank and the button is selected then the app crashed. In the console “Terminating in response to SpringBoard’s termination” is displayed.
If the variable 50 is working then there is obviously something wrong with your text field’s text. Most likely your text field is nil. Try checking back to your code and see if anything is influencing your app to become nil.
I will tell you this in case something similar happens next time. You should always try to debug your code, for example, instead of straight posting this to stack overflow with minimal code, try figuring out the problem yourself!
For example, a method identify what is wrong with your problem that I suggest you try now and feedback the results to me is checking if textfield is non-zero like below:
If the log is called in your console, you know your problem is that textfield is nil.