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)

Code Block
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

Code Block
import Foundation
let TextLabel1 = ViewController()
TextLabel1.setLabel(inputText: "Hello World") // gives Expressions are not allowed at the top level






Answered by Claude31 in 654872022
In this code, you don't need to initialise name (with the label).
Code Block
@IBAction func pushedResetButton(_ sender: Any) {
var name = textLabel.stringValue
name = "Button Reset is pushed"
textLabel.stringValue = name
}

Could have just
Code Block
@IBAction func pushedResetButton(_ sender: Any) {
textLabel.stringValue = "Button Reset is pushed"
}

Here is an example on how to call the func:
Code Block
@IBAction func pushedResetButton(_ sender: Any) {
setLabel(inputText: "Button Reset is pushed")
}

Several comments on your initial question
Code Block
import Foundation
let TextLabel1 = ViewController()
TextLabel1.setLabel(inputText: "Hello World") // gives Expressions are not allowed at the top level

You cannot set a value outside of a class or func.
What you do usually is to declare:
Code Block
    var textLabel1 : ViewController! // or textLabel1 : ViewController?

Then, set its value in a viewDidload
Code Block
textLabel1 = ViewController()

Now, you can call
Code Block
textLabel1.setLabel(inputText: "Hello World")

Your ViewController has outlets, don't initialise this way, but instantiate from storyboard.
How do you go to textLabel1 ? Through a segue link ?

Note:
TextLabel1 is not a class but a var. Should start with lowercase.
Name a ViewController as textLabel may not be the best choice.

Executable statements, such as TextLabel1.setLabel(inputText: "Hello World") are not allowed to be placed on the top level, as the error message is saying.

Such statements needs to be in a function or some closure.
Code Block
import Foundation
let textLabel1 = ViewController()
func mySetLabel() {
textLabel1.setLabel(inputText: "Hello World") //<- This is not at the top level
}

This will compile, but may not run as you expect.


how can I call this function?

It depends on when you want to call the function.
Accepted Answer
In this code, you don't need to initialise name (with the label).
Code Block
@IBAction func pushedResetButton(_ sender: Any) {
var name = textLabel.stringValue
name = "Button Reset is pushed"
textLabel.stringValue = name
}

Could have just
Code Block
@IBAction func pushedResetButton(_ sender: Any) {
textLabel.stringValue = "Button Reset is pushed"
}

Here is an example on how to call the func:
Code Block
@IBAction func pushedResetButton(_ sender: Any) {
setLabel(inputText: "Button Reset is pushed")
}

Several comments on your initial question
Code Block
import Foundation
let TextLabel1 = ViewController()
TextLabel1.setLabel(inputText: "Hello World") // gives Expressions are not allowed at the top level

You cannot set a value outside of a class or func.
What you do usually is to declare:
Code Block
    var textLabel1 : ViewController! // or textLabel1 : ViewController?

Then, set its value in a viewDidload
Code Block
textLabel1 = ViewController()

Now, you can call
Code Block
textLabel1.setLabel(inputText: "Hello World")

Your ViewController has outlets, don't initialise this way, but instantiate from storyboard.
How do you go to textLabel1 ? Through a segue link ?

Note:
TextLabel1 is not a class but a var. Should start with lowercase.
Name a ViewController as textLabel may not be the best choice.

Thank you for answering.
As in the first answer (from @claude31) I uderstand it is not possible to change a value that is inside a class.
So does this also mean I need to do all the logic within the ViewController class?

From the second answer (from @OOPer) I can see that I can get rid of the "Top level expression not allowed" message.
But as soon as you try to call the function, you have the same problem again.
So here again that all the logic needs to be done within the ViewController class?

Just before I try out some things, is it possible for example to call a function outside de ViewController class, that returns value?
That value will then be used inside the ViewCntroller class to change the label.
There is no direct way by changing the label. Or is there? I mean, there is no direct way to say "let label="text""?







So here again that all the logic needs to be done within the ViewController class? 

As I have already said, you need clarify when you want to call your logic.
For example, if you want to do something a little before a view controller is shown, viewDidLoad() in the ViewController class would be the right place.
Beginners Question (Expressions are not allowed at the top level)
 
 
Q