I have two textFields where the user types into some words.
Then, when pressing a button, i want the words to randomize and then the user displays the randomized result.
I have this code:
-(IBAction) randomInp{
NSString *first = firstField.text;
NSString *second = secondField.text;
NSString *result = //here it should randomize the words
//Display randomized word
textview.text = //should display result
}
where firstField and secondField are respectively the first and the second UITextFields. Then i don’t know how to proceed!
I was thinking of set a switch condition. If it’s 0 then returns *first, if it’s 1 then returns *second. Am i right?
Any help appreciated
EDIT
Solved!
If anyone needs:
-(IBAction) randomInp{
NSString *first = firstField.text;
NSString *second = secondField.text;
int text = rand() % 2;
switch (text) {
case 0:
textview.text = first;
break;
case 1:
textview.text = second;
break;
default:
break;
}
}
EDIT 2
The answer that SSteve gave works great, too! For anyone who needs:
NSString *result = random() & 1 ? first : second;
To choose between two values you can use
random()and check the value of a bit:Put a call to srandomdev() somewhere in your initialization code to avoid having the same sequence of values every time your program runs. You may also need
#include <stdlib.h>