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

I don't know what I am doing wrong.

Even though Xcode will generate the code for IBOutlets automatically, it's good to be familiar with the syntax. In the editor below, you have a UIViewController subclass.

Your task is to add an IBOutlet stored property to the class named textLabel of type UILabel. Remember that IBOutlets are generated with an exclamation mark at the end.

ViewController.swift
class ViewController: UIViewController {

  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.
  } @IBOutlet weak var textLabel: UILabel!
}

1 Answer

David Papandrew
David Papandrew
8,386 Points

Here is the code with correct placement of the textLabel:

class ViewController: UIViewController {
  @IBOutlet weak var textLabel: 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.
  } 
}

Even though the code looks different than previous Swift examples, you are still working with a class. In this case, it's a class called "ViewController". The layout of the class will still be like other classes you are familiar with. And the textLabel is essentially a stored property of the class.

Namely:

class sampleClass {
  //Stored properties go here: e.g. variables/constants

  //Init methods can go here (if needed)

  //Class methods go here
}