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.

iOS

Kyle Lambert
Kyle Lambert
1,969 Points

Hello World App with Swift 2.0

Hey guys, I've almost finished my Hello World app, its working but its changing UILabel to something strange, hope someone can help out...

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var nameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func helloWorldAction(nameTextField: UITextField) {
        nameLabel.text = "Hi \(nameTextField.text)"
    }

}

The code runs fine but when I enter my name into the text field, my nameLabel should change to 'Hi Kyle' but it changes to 'Hi Optional("Kyle")'.

If someone knows whats going on here, help a brother out. Thanks :)

4 Answers

Greg Kaleka
Greg Kaleka
39,018 Points

Hi Kyle,

UITextField has a property text, which you're accessing. However, the field is of type Optional(String), meaning it's an optional value, and could be nil. This is a very common thing in Swift, and you'll need to deal with optionals a lot in your code.

Aaron showed you the simplest way to do this. Adding an ! to the optional value will "force-unwrap" the optional, and convert it into a non-optional String.

forceUnwrap.swift
    @IBAction func helloWorldAction(nameTextField: UITextField) {
        nameLabel.text = "Hi \(nameTextField.text!)"
    }

This is dangerous, though, since trying to unwrap a value that actually is nil will crash your program.

Given you're writing a Hello World program, I know you're new to this, so this is probably a bit advanced, but you'll need to quickly get into the habit of properly managing optionals. Here's how I would handle this:

safer.swift
@IBAction func helloWorldAction(nameTextField: UITextField) {
    if let nameText = nameTextField.text { // This checks if the text property is nil, and saves it in nameText if not
        nameLabel.text = "Hi \(nameTextField.nameText)" // nameText is not an optional, so no ! needed
    } else { // it was nil! good thing we didn't try to unwrap it.
        nameLabel.text = "Hi there!" // Not ideal, but better than a crash.
    }
}

You'll learn more about this as you progress in iOS development, but hopefully this is a nice preview :smile:.

Happy Coding :thumbsup:

-Greg

I like your answer better :)

Kyle,

You need to unwrap that value using the ! symbol.

    @IBAction func helloWorldAction(nameTextField: UITextField) {
        nameLabel.text = "Hi \(nameTextField.text!)"
    }
Kyle Lambert
Kyle Lambert
1,969 Points

Thanks you everyone, yeah I completely understand now.. I remembered that our course teacher told us never to use force unwrapping, but I couldn't remember how to get around this issue. Seeing this is the case, you could also use a 'guard' ? Thanks

Greg Kaleka
Greg Kaleka
39,018 Points

Yes absolutely. Didn't want to throw a guard statement at you if you hadn't seen one already, but it would look like this:

safer.swift
@IBAction func helloWorldAction(nameTextField: UITextField) {
    guard let nameText = nameTextField.text else {
        nameLabel.text = "Hi there!"
    }
    nameLabel.text = "Hi \(nameTextField.nameText)"
}

Definitely cleaner - the thing we want to do isn't trapped in an if statement, and if something goes wrong, we're exiting early.

Kyle Lambert
Kyle Lambert
1,969 Points

Okay awesome, thanks so much Greg! Been very helpful :)