iOS Build a Simple iPhone App with Swift View Controllers and Views Creating IBOutlets

alexander88
seal-mask
.a{fill-rule:evenodd;}techdegree
alexander88
UX Design Techdegree Student 8,989 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
seal-mask
.a{fill-rule:evenodd;}techdegree
alexander88
UX Design Techdegree Student 8,989 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.