gpt4 book ai didi

arrays - 如何快速初始化我的数组

转载 作者:行者123 更新时间:2023-11-28 12:39:20 25 4
gpt4 key购买 nike

我有这个代码用于我想制作的问答应用程序。它应该从数组中随机询问一个问题。我的代码在 Playground 上运行,但是当我将它放入 xcode 项目时,它告诉我:

不能在属性初始值设定项中使用实例成员“Kat1”;属性初始值设定项在“self”可用之前运行

我刚开始编写应用程序和 swift,我不知道如何以正确的方式初始化数组。

这是我使用的代码:

var Kat1: [(question: String, answer: String)] =
[
("What is the capital of Alabama?", "Montgomery"),
("What is the capital of Alaska?", "Juneau"),
("What is the capital of Test?", "Test Town")
]

var antal = (Kat1.count)
var randomtal = Int(arc4random_uniform(UInt32(antal)))

print(Kat1[randomtal].question)
print(Kat1[randomtal].answer)`

我做错了什么?

最佳答案

把你的代码放在一个函数里....

   override func viewDidLoad() {
super.viewDidLoad()

var Kat1: [(question: String, answer: String)] =
[
("What is the capital of Alabama?", "Montgomery"),
("What is the capital of Alaska?", "Juneau"),
("What is the capital of Test?", "Test Town")
]

var antal = (Kat1.count)
var randomtal = Int(arc4random_uniform(UInt32(antal)))

print(Kat1[randomtal].question)
print(Kat1[randomtal].answer)`
}

关于arrays - 如何快速初始化我的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39876893/

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