Xcode won't recognise my UILable

So I've just started coding in Swift and am now trying to create a calculator which displays the number on top. To display the number I use an UILable and I then want to change its text to different numbers like this

Code Block    
@IBOutlet weak var numberLable: UILabel!
  numberLable.text="\(number)"

however line three gives me the errors


Multiline Consecutive declarations on a line must be separated by ';'
Expected '(' in argument list of function declaration
Expected '{' in body of function declaration
Expected 'func' keyword in instance method declaration
Expected declaration
Invalid redeclaration of 'numberLable()'

so I'm missing something. The grey circle is filled on line one.

Thanks for any help!
With kind regards Karl





Accepted Reply

Let’s put your code in context. You’re probably writing something like this:
Code Block swift
import UIKit
let number = 42
class MyViewController: UIViewController {
    @IBOutlet weak var numberLable: UILabel!
    numberLable.text="\(number)"
}

The problem here is that a class declaration can only directly contain declarations of the properties, methods, etc. that belong to the class. Line 8 is a statement—a particular bit of code that needs to execute, not just a declaration that something exists so it be used elsewhere—so you can’t write it there. You need to put it in the body of a method or some other declaration that then gets called; that way Swift will know when you want it to run.

For example, you might write it in viewDidLoad(), so it happens as soon as numberLable is set:
Code Block swift
class MyViewController: UIViewController {
    @IBOutlet weak var numberLable: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
    numberLable.text="\(number)"
}
}

Or you could put it in an IBAction method and then connect it to a “Refresh” button using Interface Builder:
Code Block swift
class MyViewController: UIViewController {
    @IBOutlet weak var numberLable: UILabel!
@IBAction func refreshNumber() {
    numberLable.text="\(number)"
}
}

Or you could put it in its own method that you call whenever you need to update the number, which is a great idea if there are several different places where you need to update the text:
Code Block swift
class MyViewController: UIViewController {
    @IBOutlet weak var numberLable: UILabel!
var number: Int {
didSet { updateNumberLable() }
}
override func viewDidLoad() {
super.viewDidLoad()
updateNumberLable()
}
func updateNumberLable() {
// updateNumberLable() could get called before numberLable
// is set, so we need to check for that and skip trying to
// update the text if it’s still nil.
guard numberLable != nil else { return }
    numberLable.text="\(number)"
}
}

I hope that helps you understand what’s going on!



As you probably noticed, Swift’s error messages for this mistake were not very helpful. Any time you feel like a Swift error message is confusing or not helpful, please feel free to file that as a bug in Feedback Assistant, and attach a copy of your code with the mistake still in it so we can see what happened. The Swift compiler needs to be specifically programmed to recognize mistakes like these—otherwise it tends to produce “technically correct” but confusing messages like you saw here—so examples of things we forgot to check for are incredibly helpful!

Replies

You may be missing some very basic thing in Swift.

Executable statement like numberLable.text="\(number)" should be placed in a method (or an initializer or a closure).

Try moving the line into viewDidLoad().
Let’s put your code in context. You’re probably writing something like this:
Code Block swift
import UIKit
let number = 42
class MyViewController: UIViewController {
    @IBOutlet weak var numberLable: UILabel!
    numberLable.text="\(number)"
}

The problem here is that a class declaration can only directly contain declarations of the properties, methods, etc. that belong to the class. Line 8 is a statement—a particular bit of code that needs to execute, not just a declaration that something exists so it be used elsewhere—so you can’t write it there. You need to put it in the body of a method or some other declaration that then gets called; that way Swift will know when you want it to run.

For example, you might write it in viewDidLoad(), so it happens as soon as numberLable is set:
Code Block swift
class MyViewController: UIViewController {
    @IBOutlet weak var numberLable: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
    numberLable.text="\(number)"
}
}

Or you could put it in an IBAction method and then connect it to a “Refresh” button using Interface Builder:
Code Block swift
class MyViewController: UIViewController {
    @IBOutlet weak var numberLable: UILabel!
@IBAction func refreshNumber() {
    numberLable.text="\(number)"
}
}

Or you could put it in its own method that you call whenever you need to update the number, which is a great idea if there are several different places where you need to update the text:
Code Block swift
class MyViewController: UIViewController {
    @IBOutlet weak var numberLable: UILabel!
var number: Int {
didSet { updateNumberLable() }
}
override func viewDidLoad() {
super.viewDidLoad()
updateNumberLable()
}
func updateNumberLable() {
// updateNumberLable() could get called before numberLable
// is set, so we need to check for that and skip trying to
// update the text if it’s still nil.
guard numberLable != nil else { return }
    numberLable.text="\(number)"
}
}

I hope that helps you understand what’s going on!



As you probably noticed, Swift’s error messages for this mistake were not very helpful. Any time you feel like a Swift error message is confusing or not helpful, please feel free to file that as a bug in Feedback Assistant, and attach a copy of your code with the mistake still in it so we can see what happened. The Swift compiler needs to be specifically programmed to recognize mistakes like these—otherwise it tends to produce “technically correct” but confusing messages like you saw here—so examples of things we forgot to check for are incredibly helpful!
Thank you so much! This makes allot of sense.
Have a good friday!