Swift:预期的声明错误设置“标签"到一个字符串变量 [英] Swift: expected declaration error setting "Label" to a string variable

查看:70
本文介绍了Swift:预期的声明错误设置“标签"到一个字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个小型的单窗格应用程序中管理不同的语言,为每个注释使用不同的字符串数组,并由整数变量"userLang"索引,然后设置label.text = array [index].基本代码是:

I am managing different languages, in a small single pane app, using a different string array for each comment, indexed by an integer variable "userLang", then setting label.text = array[index]. The basic code is:

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 an "Expected Declaration" error. I have tried using a simple string in quotes (instead of the array) as a test, and still get the same error. I have searched the web & not found any suggestions that resolve the problem. I tried changing the name if the label, in case I was using "reserved" references.

我在Apple的开发人员手册中搜索了Swift&找不到标签的正确语法.此语法已在其他项目中使用,所以我不确定发生了什么.我什至尝试将相关部分复制/粘贴到新项目中(在线建议之一,以防Xcode错误),但没有更好的结果.我注意到小差异(空格或大写)可以在Swift中产生很大的差异.我在这里做错什么了吗?有什么建议吗?

I searched Apple's developer manual for Swift & can't find the proper syntax for labels. This syntax has worked in other projects, so I am not sure what is going on. I even tried copy/pasting relevant sections to a new project (one of the online suggestions, in case of Xcode bug), with no better results. I have noticed small differences (a space or capital) can make a big difference in Swift. Is there something I am doing wrong here? Any suggestions?

推荐答案

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

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

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

这篇关于Swift:预期的声明错误设置“标签"到一个字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆