Swift: expected installation of a Label declaration error for a string variable

I manage different languages ​​in a small application with one panel, using a different string array for each comment, indexed by the integer variable userLang, and then setting label.text = array [index]. Main code:

import UIKit class ViewController: UIViewController { var userLang = 0 var arrayOne = ["hi", "hola"] var arrayTwo = ["Bye", "Adios"] @IBOutlet weak var msgArrayOne: UILabel! @IBOutlet weak var msgArrayTwo: UILabel! msgArrayOne.text = arrayOne[userLang] //Error here: !Expected declaration msgArrayTwo.text = arrayTwo[userLang] //odd, but this line gets no error until I //remove the first line above, then //this line gets the same error. override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

I keep getting the "Pending declaration" error. I tried using a simple quoted string (instead of an array) as a test and still getting the same error. I searched the Internet and did not find any suggestions that solve the problem. I tried to change the name if the label, in case I used the "reserved" links.

I was looking for the Apple developer guide for Swift and could not find the correct shortcut syntax. This syntax worked in other projects, so I'm not sure what is going on. I even tried to copy / paste the appropriate sections into a new project (one of the suggestions online, in case of an Xcode error), without any better results. I noticed that small differences (space or capital) can make a big difference in Swift. Is something I am doing wrong here? Any suggestions?

+8
string swift labels
source share
1 answer
 msgArrayOne.text = arrayOne[userLang] msgArrayTwo.text = arrayTwo[userLang] 

These are not declarations, you will need to move them to viewDidLoad () or some other suitable place. Your syntax for labels is fine, but you have the code in the wrong place in the class.

+22
source share

All Articles