Swift:预期声明错误将“Label”设置为字符串变量

前端之家收集整理的这篇文章主要介绍了Swift:预期声明错误将“Label”设置为字符串变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在一个小的单窗格应用程序中管理不同的语言,为每个注释使用不同的字符串数组,由整数变量“userLang”索引,然后设置label.text = array [index].基本代码是:
  1. import UIKit
  2. class ViewController: UIViewController {
  3. var userLang = 0
  4. var arrayOne = ["hi","hola"]
  5. var arrayTwo = ["Bye","Adios"]
  6. @IBOutlet weak var msgArrayOne: UILabel!
  7. @IBOutlet weak var msgArrayTwo: UILabel!
  8.  
  9. msgArrayOne.text = arrayOne[userLang] //Error here: !Expected declaration
  10. msgArrayTwo.text = arrayTwo[userLang] //odd,but this line gets no error until I
  11. //remove the first line above,then
  12. //this line gets the same error.
  13.  
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. // Do any additional setup after loading the view,typically from a nib.
  17. }
  18.  
  19. override func didReceiveMemoryWarning() {
  20. super.didReceiveMemoryWarning()
  21. // Dispose of any resources that can be recreated.
  22. }
  23. }

我一直收到“预期声明”错误.我尝试在引号(而不是数组)中使用一个简单的字符串作为测试,但仍然得到相同的错误.我搜索过网络&找不到任何解决问题的建议.如果我使用“保留”引用,我尝试更改名称,如果标签.

我在Apple的开发者手册中查找了Swift&找不到标签的正确语法.这种语法在其他项目中有效,所以我不确定是怎么回事.我甚至尝试将相关部分复制/粘贴到一个新项目(一个在线建议,如果是Xcode bug),没有更好的结果.我注意到小差异(空间或资本)可以在Swift中产生很大的不同.我在这里做错了吗?有什么建议么?

  1. msgArrayOne.text = arrayOne[userLang]
  2. msgArrayTwo.text = arrayTwo[userLang]

这些不是声明,您需要将它们移动到viewDidLoad()或其他适当的位置.您的标签语法很好,但是您将代码放在类中的错误位置.

猜你在找的Swift相关文章