gpt4 book ai didi

string - Swift:预期声明错误将 "Label"设置为字符串变量

转载 作者:IT王子 更新时间:2023-10-29 05:46:47 25 4
gpt4 key购买 nike

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

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.
}
}

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

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

最佳答案

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

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

关于string - Swift:预期声明错误将 "Label"设置为字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26764266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com