I have two variables, string0 and string1. I want to randomly set a label to one of those strings. I tried generating the variable name using a random number like this:
let string0a = "\(Name!) sees something that offends \(Gender!)."
let string1a = "\(Name!) saw Star Wars earlier."
let number = arc4random_uniform(1)
text1.text = string\(number!)a
What is the best way to randomly set the label text to one of the two strings?
You can't generate variable names at runtime in Swift because they are used at compile time. This is what arrays were invented for:
let strings = [string0a, string1a]
let number = Int(arc4random_uniform(2))
text1.text = strings[number]
or more concisely:
text1.text = [string0a, string1a][Int(arc4random_uniform(2))]
Related
I am building an app that the user selects a multiplication table. Then it gives random numbers to multiplicate with the number they select. for example, if the user selects "1". the questions shown would be "1 x 1", or "1 x 8".
The problem is that I need to assign the same random number to 2 variables. The one that will be shown on the question and the one used to calculate the result.
I thought of something like this, but the random number is different on each variable. What can I do to use the same random number generated on 2 variables?
func game() -> (String, Int) {
let randomNumber = multiplicate.randomElement()
switch selectedQuestion {
case 0:
return ("1 × \(randomNumber!)", 1 * randomNumber!)
default:
return ("", 0)
}
}
Not exactly sure what you're asking but you could do something like this:
let number = Int.random(in: 0..<10)
let number2 = number
but I don't think there is a need to create a new variable for this. The whole idea of variables is that you can save some value and then use it later so there isn't really a need to create number2 here.
I have been attempting to Add the values of some UI labels in Xcode, but with '+' there is an error and I have not found any other way yet to have it work.
Storyboard
ViewController.Swift
It‘s not clear in your question but I think you’re trying to change the text displayed by the UILabel. If this is the case, you can do this as following
myLabel.text = “myText”
or
myLabel.text += “Test”
You have to be more specific. I don’t really get what you want to add so I will assume.
If you want to add the Strings the UILabels contain, you should do as Bruce said above.
If you want to add the two strings into a variable you should do this.
Var outcome = label1.text + label2.text
The outcome would be the string of label one plus the string of label two connected.
Now if the labels contain numbers and you want to add those numbers you should do this. (Assuming they do contain Integers)
let num1 = label1.text.toInt()
let num2 = label2.text.toInt()
let outcome = num1 + num2
In your case it would be....:
Let sumOfIntegers = MathCriAValue.text.toInt() + MathCriBValue.text.toInt() + MathCriCValue.text.toInt() + MathCriDValue.text.toInt()
I am trying to store all numbers that the random number generator generate. After that the number generator needs to check if the number already was generated and if so it will keep generate a new number until all number for example 1 to 30 are generated. I have so far only the random number generator:
if let Aantalvragen = snapshot?.data()!["Aantal vragen"] as? String {
self.AantalVragenDef = Aantalvragen
}
let RandomVraag = Int(arc4random_uniform(UInt32(self.AantalVragenDef)!) + 1)
AantalVragenDef is an number that indicates how many questions there are. So the generator knows how far it can generate. Please help.
The easiest is probably to create an array or list and fill it with the numbers 1 to n that you want, shuffle it and then use the numbers in the order they appear. That way you are guaranteed that each number show up exactly once.
See how to shuffle an array in Swift
I believe what you are trying to get is a random generator that generates numbers from 1 to the number of questions, but if the number already exists, you don't want to keep it. I suggest using if-else statements and arrays.
The code might look something like this:
if let Aantalvragen = snapshot?.data()!["Aantal vragen"] as? String {
self.AantalVragenDef = Aantalvragen
}
var array = [Int]()
while array.count != self.AantalVragenDef {
let RandomVraag = Int(arc4random_uniform(UInt32(self.AantalVragenDef)!) + 1)
if array.contains(RandomVraag) == false {
array.append(RandomVraag)
}
}
This loop will continue until there are (number of questions) integers in the array. Let me know if this is what you are looking for.
Good Luck, Arnav
i have 3 Doubles, their values are random, and i am trying to achieve a comparison between them
so my code is
let double1 = 10.00 // notes: they're all random between 1-100 so i don't know what this will be
let double2 = 7.66
let double3 = 6.43
but, i tried
if (double1?.isLess(than: 50.0))! {
print("Low")
} else {
print("High")
}
but as i mentioned, this can't be done due to the random values, i need to compare two of them to each other, to make sure that one of them will be higher
Its actually rather very simple:
let maxDouble = max(max(double1, double2), double3) // prints 10
How to concatenate string with brackets in Swift 2
in simple i know how to concatenate the string but if i want to brackets inside the string
let a = "Hello"
let b = "World"
let first = "(a) Per Level (b)" // i want to show this output ?
output would be like this : (Hello) Per Level (World)
Another format you can use with the latest Swift2:
let first = "(\(a)) Per Level (\(b))"
Try this:
let first = String(format: "(%#) Per Level (%#)", a, b)