Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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)


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