首页 > 解决方案 > 如何设置新值以更改我的 Story 应用程序的问题

问题描述

我想根据用户的选择来改变我的故事。但是,每次我运行该应用程序时,它总是显示相同的问题。具体来说,第 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.", choice1Destination: 2,
    choice2: "Wait, I know how to change a tire.", choice2Destination: 3
),

标签: swiftvariablesfunc

解决方案


一种选择是在决定选择哪个故事时使用随机数:

let index = Int.random(0..<hikayeler.count)

另一种选择是记录用户看过哪些故事,以确保他们在启动时总是得到不同的故事。

由您决定您希望应用程序如何工作。

编辑 - 为了让故事继续下去,您需要一个存储当前索引的变量。

class ViewController: UIViewController {
    var index = 0

    @IBAction func button1(_ sender: UIButton) {
        let variable1 = hikayeler[index]
        ...
        index = variable1
    }
}

推荐阅读