Post

Replies

Boosts

Views

Activity

Beginners Question (Expressions are not allowed at the top level)
Hi I'm a beginner in programming. I do have programming experiance but not with Object Oriented languages. Followed some online courses and wanted to try this out. I made an simple Story board in Xcode with a button and a Label. And I connected that all the ViewController Class. When I push the button, the label text is changed. So far so good. Then I added a function that accepts a String. But how can I call this function? I get a "Expressions are not allowed at the top level". Tried to search internet, but couldnt find the answer I'm looking for. Can someone explain what I need to do to call this setLabel function? Here's my code: --- "Viewcontroller.swift" (The default created template) import Cocoa class ViewController: NSViewController { @IBOutlet weak var resetButton: NSButton! @IBOutlet weak var textLabel: NSTextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override var representedObject: Any? { didSet { // Update the view, if already loaded. } } // Reset button is pushed here. @IBAction func pushedResetButton(_ sender: Any) { var name = textLabel.stringValue name = "Button Reset is pushed" textLabel.stringValue = name } // Call this function to change text in label. func setLabel(inputText: String){ var name = textLabel.stringValue name = inputText textLabel.stringValue = name } } ------- ChangeText.swift (my swift code 	import Foundation 	let TextLabel1 = ViewController() TextLabel1.setLabel(inputText: "Hello World") // gives Expressions are not allowed at the top level
4
0
2.5k
Dec ’20