Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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/1/vb.net/16.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
Swift 基于类变量的字典随机字符串_Swift - Fatal编程技术网

Swift 基于类变量的字典随机字符串

Swift 基于类变量的字典随机字符串,swift,Swift,我通过创建一个随机生成的文本冒险来练习swift,如果你可以这么说的话 positionArr是一个二维数组,包含类房间的实例,其中每个单元格填充随机生成的房间,属性为温度和在之前输入,跟踪您是否在房间内currentRow/currentCollum记录您所在的房间。使用下面的func returnText(),我希望根据roomReference,即您当前的房间,随机生成要输出的文本 func returnText() { var roomReference = positionA

我通过创建一个随机生成的文本冒险来练习swift,如果你可以这么说的话

positionArr
是一个二维数组,包含类
房间
的实例,其中每个单元格填充随机生成的
房间
,属性为
温度
之前输入,跟踪您是否在房间内
currentRow
/
currentCollum
记录您所在的房间。使用下面的
func returnText()
,我希望根据
roomReference
,即您当前的房间,随机生成要输出的文本

func returnText()
{

    var roomReference = positionArr[currentRow][currentCollum]

        var tempature =
        [
            true: ["It's cold Outside","it's warm"],
            false: ["It's warm Outside", "it's cold"]
        ]

        var entered =
        [
            true: ["You have been here before","This isn't the first time you have been here before"],
            false: ["This room seems new","This is the first time you have been here"]
        ]
使用字典
temperature
entered
以及我正在寻找的变量,随机选择正确的属性,如果您进入一个寒冷的房间,即小于0,则为
true
并选择一个“寒冷”短语

    //This is pseudo of what I am trying to do

    var text1 = temperature[roomReference.temperature <= 0].getRandom()

   //if room tempature is less than zero, it is TRUE, therfore grabbing cold text from the dictionary

    var text2:String = entered[roomReference.enteredBefore].getRandom()
//这是我试图做的伪操作

var text1=temperature[roomReference.temperature这可能是一个更复杂的问题,有无数合理的实现。要回答第一步,访问数组的随机成员:

myArray[random() % myArray.count]
这将返回一个合理的随机字符串