我有這個問題和答案應用程序的代碼,我想。它應該問陣列中的一個隨機問題。 我的代碼在操場上工作,但是當我將它放入一個xcode項目時,它告訴我:如何在swift中初始化我的數組
無法在屬性初始值設定項中使用實例成員'Kat1'屬性初始值設定項在'self'可用之前運行
Im新編程應用程序和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)`
我到底做錯了什麼?
我相信你需要把它放在一個函數或viewDidLoad中()內 – Starlord
記住arc4Random不是真正隨機的,你可能會一遍又一遍地結束了相同的號碼。 – Starlord
@Joakim不,「rand」是僞隨機的,但「arc4random_uniform」是隨機的。 – Moritz