Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Mart Gaedtgens
Mart Gaedtgens
2,251 Points

Integer value from a text label

So I tried to get in some practice after watching the iOS beginners course. But in my project I want to get a random number, and that random number's upperbound needs to be the input from the text label. Everytime I try something to convert the text label to an integer value, i.e.,

let number = Int(txtLabel.text!) [I know im not supposed to use ! but just for now] let randomNumber = arc4random_uniform(number)

it says: Cannot convert value of type 'Int?' to expected argument type 'UInt32' I have no idea what this means. Could someone please help me out so that the input from the textlabel will work as the Upperbound of the random number.

1 Answer

Try this.

let numString = "10"
if let numInt = Int(numString){
    let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: numInt)
}

Or

let numString = "10"
if let numInt = UInt32(numString) {
    let randomNumber = Int(arc4random_uniform(numInt))
}