Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 重置Swift数组_Arrays_Swift - Fatal编程技术网

Arrays 重置Swift数组

Arrays 重置Swift数组,arrays,swift,Arrays,Swift,我有真理和勇气的阵列,我希望能够在按下“新游戏”按钮后重置阵列 数组的一些背景信息:数组在初始值设定项中被洗牌,然后每次按下Dare或Truth按钮时,它都会在数组中移动,并将提示放入标签中 我基本上是在提示下尝试在swift文件中重新初始化数组,但我无法将按钮连接到该文件(可能您只能在视图控制器中执行此操作?这是我的第一个实际项目,因此我对它非常不熟悉) 然后,我尝试在视图控制器中重新初始化提示文件,但似乎无法正确引用它 import GameKit struct PromptProvide

我有真理和勇气的阵列,我希望能够在按下“新游戏”按钮后重置阵列

数组的一些背景信息:数组在初始值设定项中被洗牌,然后每次按下Dare或Truth按钮时,它都会在数组中移动,并将提示放入标签中

我基本上是在提示下尝试在swift文件中重新初始化数组,但我无法将按钮连接到该文件(可能您只能在视图控制器中执行此操作?这是我的第一个实际项目,因此我对它非常不熟悉)

然后,我尝试在视图控制器中重新初始化提示文件,但似乎无法正确引用它

import GameKit

struct PromptProvider {
    var truths = [
        "Truth A",
        "Truth B",
        "Truth C",
        "Truth D",
        "Truth E"]
    var dares = [
        "Dare A",
        "Dare B",
        "Dare C",
        "Dare D",
        "Dare E"]

    init() {
        self.enabledTruths = enabledTruths.shuffled()
        self.enabledDares = enabledDares.shuffled()
        }


    var truthIndex = 0
    mutating func randomTruth() -> String {
        if truthIndex == truths.count - 1 {
            return "You're out of truths. Click 'New Game' in the menu section." } else {
            truthIndex += 1
            return truths[truthIndex]
        }
    }
    var dareIndex = 0
    mutating func randomDare() -> String {
        if dareIndex == dares.count - 1 {
            return "You're out of dares. Click 'New Game' in the menu section." } else {
            dareIndex += 1
            return dares[dareIndex]
        }
    }
}

基本上,我只需要重置数组以在索引0处重新开始(最好是重新洗牌)

将重新初始化代码移动到名为
restart()
变异函数。从
init()
调用它。当您想通过调用
provider.restart()
重置
提供程序时,也可以调用它

注意:要访问所有的
真相
数据
,我们使用
延迟
语句返回值后增加索引


示例:

var provider = PromptProvider()

for _ in 1...6 {
    print(provider.randomTruth())
    print(provider.randomDare())
}
var provider = PromptProvider()

for _ in 1...6 {
    print(provider.randomTruth())
    print(provider.randomDare())
}
Truth D
Dare C
Truth E
Dare A
Truth C
Dare B
Truth A
Dare E
Truth B
Dare D
You're out of truths. Click 'New Game' in the menu section.
You're out of dares. Click 'New Game' in the menu section.
provider.restart()

for _ in 1...6 {
    print(provider.randomTruth())
    print(provider.randomDare())
}
Truth D
Dare B
Truth A
Dare A
Truth E
Dare C
Truth C
Dare D
Truth B
Dare E
You're out of truths. Click 'New Game' in the menu section.
You're out of dares. Click 'New Game' in the menu section.