问题描述
我想根据用户选择更改故事。但是,每次我运行该应用程序时,它总是显示相同的问题。具体来说,是第30行和第41行。我不想在那儿写0,但IDK怎么办。我确实拥有不混淆的“结构”。
var hikayeler = [
Story(
title: "Your car has blown a tire on a winding road in the middle of Nowhere with no cell phone reception. You decide to hitchhike. A rusty pickup truck rumbles to a stop next to you. A man with a wide brimmed hat with soulless eyes opens the passenger door for you and asks: 'Need a ride,boy?'.",choice1: "I'll hop in. Thanks for the help!",choice1Destination: 2,choice2: "Better ask him if he's a murderer first.",choice2Destination: 1
),Story(
title: "He nods slowly,unfazed by the question.",choice1: "At least he's honest. I'll climb in.",choice2: "Wait,I kNow how to change a tire.",choice2Destination: 3
),
解决方法
一种选择是在决定选择哪个故事时使用随机数:
let index = Int.random(0..<hikayeler.count)
另一种选择是记录用户看到过哪些故事,以确保他们在启动时总是得到不同的故事。
由您决定要如何运行您的应用。
编辑-为了使故事继续进行,您需要一个存储当前索引的变量。
class ViewController: UIViewController {
var index = 0
@IBAction func button1(_ sender: UIButton) {
let variable1 = hikayeler[index]
...
index = variable1
}
}