swift - 如何设置新值以更改我的 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
),
解决方案
一种选择是在决定选择哪个故事时使用随机数:
let index = Int.random(0..<hikayeler.count)
另一种选择是记录用户看过哪些故事,以确保他们在启动时总是得到不同的故事。
由您决定您希望应用程序如何工作。
编辑 - 为了让故事继续下去,您需要一个存储当前索引的变量。
class ViewController: UIViewController {
var index = 0
@IBAction func button1(_ sender: UIButton) {
let variable1 = hikayeler[index]
...
index = variable1
}
}
推荐阅读
- azure-active-directory - 如何将组织者添加到使用 Microsoft Graph API 创建的在线会议
- javascript - 从 node.js google drive api 返回结果
- redirect - 如何在邮递员中访问重定向的 url?
- ios - 保存文档 ID,以便它可以在整个 ViewController 中使用
- c++ - 如何在不复制的情况下插入 DIY 链表实现?
- asp.net-core - 带有实体框架的 SignalR-Hub 返回错误的实体
- javascript - IE 无法获取 xml+svg 格式的图像大小?
- sql - 如何检索每个不同 ID 的最早日期和状态
- php - 在我的网站上实施用户实时状态跟踪的最佳方式是什么?
- python - bingads V13 报告请求在 python sdk 中失败