As others have identified, your minRange and maxRange are Optionals. They need to be Int, not Int? in order to form the range.
Also, you should avoid using ! to unwrap the text fields, just in case they're nil.
If you have some reasonable defaults for minRange and maxRange (such as 1 and 10), you could use the nil coalescing operator to unwrap your text fields and replace the values with defaults if the text fields can't be converted to an Int:
@IBAction func generateNumbers(sender: AnyObject) {
let minRange = Int(lowValue.text ?? "") ?? 1
let maxRange = Int(highValue.text ?? "") ?? 10
func randomNumber(range: Range<Int>) -> Int {
let min = range.startIndex
let max = range.endIndex
return Int(arc4random_uniform(UInt32(max - min))) + min
}
// test
let results = randomNumber(minRange...maxRange)
self.field1.text = String(results)
}
Note: The compiler is not happy with using the values minRange and maxRange to set the default value for range, so I added them to the call of randomNumber.
There is another potential gotcha here as well. You can't form a range if minRange is larger than maxRange, so you should check for that as well:
var minRange = Int(lowValue.text ?? "") ?? 1
var maxRange = Int(highValue.text ?? "") ?? 10
if minRange > maxRange {
// Swap 'em
(minRange, maxRange) = (maxRange, minRange)
}
Why even torture yourself in creating the range anyway? Since randomNumber is declared inside of generateNumbers you can just use minRange and maxRange:
@IBAction func generateNumbers(sender: AnyObject) {
var minRange = Int(lowValue.text ?? "") ?? 1
var maxRange = Int(highValue.text ?? "") ?? 10
if minRange > maxRange {
// Swap 'em
(minRange, maxRange) = (maxRange, minRange)
}
func randomNumber() -> Int {
return Int(arc4random_uniform(UInt32(maxRange - minRange))) + minRange
}
//test
let results = randomNumber()
self.field1.text = String(results)
}