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 Build a Simple iPhone App with Swift View Controllers and Views Creating IBOutlets

alexander88
alexander88
10,824 Points

Value of type 'UILabel' has no member 'txt'

I have the following error:

Value of type 'UILabel' has no member 'txt'

This is the code I have currently:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var funFactLabel: UILabel!

override func viewDidLoad() {

super.viewDidLoad()

funFactLabel.txt = "Fun Fact!" } }

2 Answers

alexander88
alexander88
10,824 Points

It's funny but within 5 minutes of posting this separately from a related thread in hopes of getting an answer I do the exact same thing, recreate the connection from the UILabel (which I had tried already), but this time it works. I noticed however that this time it changed the following:

funFactLabel.txt = facts[0]

to:

funFactLabel?.txt = facts[0]

It should be noted that I moved ahead to the point where you create an array of facts by the time I figured this out, thus the different code.

If anyone is able to explain why that makes a difference that would be wonderful. I tried removing one "?" and the build succeeded so I'm not entirely certain what happened here.

Bronte A
Bronte A
2,972 Points

Hi :) the question mark is needed because the label is what's called an optional - it either could have a value, or it couldn't.