arc4random using input from textField - objective-c

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 (textfield) {
NSLog (#"Text field is not nil! GOOD!");
}
If the log is called in your console, you know your problem is that textfield is nil.

Related

How to convert a method name into a String

I´m developing a little game right now and I want to have several levels.
So i´m asking if it´s the first time the app gets launched. If it´s like that the user will start at level1.
But if now i want to start another method.
I have methods like:
-(void)level1{};
-(void)level2{};
.
.
.
till level 100 or so.
So after if got the amount of times the app got started i want to call a method, which fits.
So i tried to make it like:
[self [NSString stringWithString:#"level%d", level]];
Because "level" has a number like 1,2,3...,100. So i tried to make it like level1,level2, level3, and so on.
But for that I get an error -> Unexpected interface name 'NSString': expected expression.
Can anyone please help me? Thanks in advance!
Peace!
You need to use NSSelectorFromString():
NSString* methodName = [NSString stringWithFormat:#"level%d", level];
SEL sel = NSSelectorFromString(methodName);
[self performSelector:sel];

Label only displays %i instead of number

I have a method which creates one ball each time I tap on the screen. Now, I have a label which shall show the score, for now I want to let it show how many balls there are / how many times the user tapped.
The label is called score and I use the following code in the method that is responsible for the tap-ball-creation.
scoreCount += 1;
_score.text = #"%i", scoreCount;
NSLog(#"ScoreCount = %i", scoreCount);
In the debugger it shows scoreCount increasing as it should, but the label only displays %i even though scoreCount is declared in the .h as int.
I already tried to use %f and %d instead but Xcode always tells me "Expression result unused". But why?
P.S.: I'm using chipmunk stuff...
Try changing the second line to
_score.text = [NSString stringWithFormat#"%i", scoreCount];

Obj-C Text fields, float values and SIGBART

I am writing an iOS application for (among many features) calculating alcohol content from sugar before and after fermentation for homebrewers. Now the problem is; every time I run my app in the simulator, it crashes with "Thread 1: Signal SIGBART" once I enter the UIViewController with the text fields, labels and buttons used in this function (in the implementation):
- (IBAction)calcAlc:(id)sender {
double ogVal = [[oGtext text]floatValue];
double fgVal = [[fGtext text]floatValue];
double alcoContent = ((1.05*(ogVal-fgVal))/fgVal)/0.79;
NSString *theResult = [NSString stringWithFormat:#"%1.2f",alcoContent];
alcContent.text = theResult;
}
I'm really stuck here -- any help is very appreciated. Thanks in advance!
You should check if fgVal is ever 0, since you are dividing by fgVal.
You will also want to use doubleValue instead of floatValue, since you are declaring them as double.
I think you should try float in place of double.
Now put break points on each and every method and debug it to get the exact point where you application is getting crashed and then you will be able to find the solution.
Also be sure to connect each and every ib outlet and action in your storyboard or nib(whatever you are using).
Please notify if it works..:)

iPhone application error. NSString with whitespace

I am having a major problem with my iPhone app. My problem seems basic but I cannot find out the reason for the error. I have a NSString saveDescription that is set by user's input.
savede = civilsJobDescTb.text;
I then call in a NSLog to check the string has been set, and it is. So I have no problem there.
Later in the program I create a PDF file with several details. Everything works ok except the NSString that I have set earlier. It seem as if the string no longer exists. I get an error from it. EXC_BAD_ACCESS or something like that.
Now for the weird thing. This only happens when I have a whitespace in the text box. Eg: I enter "ok" and it will work without problem. But if I enter "Everything is ok" then I get errors.
I can't see where the problem is. Any ideas?
You didn't specify if your "savede" is an ivar (instance variable) but assuming that it is,
and if you are using ARC, set a strong property to it:
#property (nonatomic, strong) NSString * savede;
self.savede = civilsJobDescTb.text;
If you are not using ARC, then make sure to retain your "savede" string after setting it. Something like:
savede = civilsJobDescTb.text;
[savede retain];
(making sure to release it when you are truly finished with it).
You can also make a copy of the string (in case the label that it's coming from disappears, or is cleared, or somehow disappears).
[savede setString: civilsJobDescTb.text];
[savde retain];

Add integers from 5 UITextFields to a UILabel in Cocoa Touch

I'm trying to sum the integers from five UITextFields and post them to a UILabel.
This is the code I have tried, but it doesn't work properly. The number that shows up in the label is not the sum of my textfields. I have also tried to post to a textfield instead of a label, with the same result. No errors or warnings when I build.
int val = [textfield1.text intValue]
val = val+[textfield2.text intValue];
val = val+[textfield3.text intValue];
val = val+[textfield4.text intValue];
val = val+[textfield5.text intValue];
NSString *labelStr = [[NSString alloc] initWithFormat:#"%i", val];
label.text = labelStr;
Something wrong with the code? Alternative code? Grateful for all answers!
The code looks more or less right to me, aside from the memory leak. You should review the memory management rules and fix your leak.
My guess is that the numbers you entered add up to a number that is outside the range of an int. Entering, say, 1000000000 (10**9) in each of the five fields would be one way to pull this off, on any machine where an int is 32 bits (including, currently, the iPhone-OS devices).
Depending on the purpose of your app, you may be able to simply cap the five input fields; if the highest value that makes any sense is less than one-fifth (for five fields, and that's assuming they all have the same cap) of the maximum int, overflow is impossible.
If a cap won't solve the problem completely, try a different type. If the values should never be negative, use an unsigned type. Otherwise, try long long, or either of the floating-point types, or use NSDecimalNumber objects.
Of course, I could be completely wrong, since you didn't say what numbers you entered or what the result was. If it was zero, make sure that you hooked up your outlets in IB; if you forgot to do that, they contain nil, which, when you ask it for text, will return nil, which, when you ask it for an intValue, will return 0, and 0 + 0 + 0 + 0 + 0 = 0.