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:
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
In this code, you don't need to initialise name (with the label).
Could have just
Here is an example on how to call the func:
Several comments on your initial question
You cannot set a value outside of a class or func.
What you do usually is to declare:
Then, set its value in a viewDidload
Now, you can call
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.
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.