Search code examples
swiftstringstartupvar

How to Auto load info to label while open .app by swift code


I am starting to learn swift code, with this code after entering your name and pressing the button will appear greeting information. I want to not need to press the button, when I open the application, can it run this code by itself? with the words "Hello World!"

var name = nameField.stringValue
if name.isEmpty {
  name = "World"
}
let greeting = "Hello \(name)!"
helloLabel.stringValue = greeting

Solution

  • Put the code into viewDidLoad.

    override func viewDidLoad() {
        super.viewDidLoad()
        
        ...
    }