swiftxcodevariablescalculatoriboutlet

Unable to use IBOutlets after linking


I have already finished the design aspect and began linking the text field to my ViewController. However, when I try to perform a simple command that is changing its text, the variable doesn't seem to be defined and I get 8 errors. Even when I typed it out, it didn't appear among the many suggestions that Xcode provides to you.

Here's a picture: https://i.sstatic.net/HaqsO.png

I have made connections and changes before but for some reason, something's only going wrong now. I tried following my instructor second by second and copied everything she did, but still, I got the error while she didn't. I also cloned the project that had only the MainStoryboard completed and not the code. Again, the same error.

I'm using the latest version of Xcode that is Version 11.4.1. Am I doing something wrong?

If it helps, here's a screenshot of my entire workspace as well as the ViewController's code: https://i.sstatic.net/vFVjO.jpg

@IBOutlet weak var upperText: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

upperText.text = "test" 

Thanks.


Solution

  • You can only use variables and do calulations in functions, not directly in the class. Put the logic in a function, try this:

    @IBOutlet weak var upperText: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        upperText.text = "test" 
    }