将函数链接到字符串数组,仅将随机元素仅打印到标签上

问题描述 投票:0回答:1

以下代码存在问题,我无法使其正常工作。

我收到的错误消息是:

“无法将类型'(Any)-> Int'的值转换为预期的参数类型'Int'”

“ void函数中意外的非void返回值。”

我正在使用Single View应用程序,并且正在使用xCode(最新版本)中的“ ViewController”。

我已经尝试了一切。我对编码非常陌生,所以要表现得好像我是一只以前从未接触过计算机的无助小狗。最有可能我只是忘记了“携带1”。

@IBOutlet var testing: UILabel!
@IBAction func wow(_ sender: Any) {
    var items = ["blue", "red", "purple", "gold", "yellow", "orange",
                 "light blue", "green", "pink", "white", "black"]

    //array to hold the index we have already traversed
    var seenIndex = [Int]()


    func chooseRandom() -> String {

        if seenIndex.count == items.count { return "" } //we don't want to process if we have all items accounted for (Can handle this somewhere else)

        let index = Int(arc4random() % UInt32(items.count))
            testing.text = "\(items[index])"
    }

    //check if this index is already seen by us
    if seenIndex.contains(index) {
            return chooseRandom() //repeat
    }

    //if not we get the element out and add that index to seen
    let requiredItem = items[index]
    seenIndex.append(index)
    return requiredItem
}

我希望结果将“ blue”或数组的任何其他元素打印到标签中,但是永远不要再打印“ blue”,直到列表用完并且“重置”为止。

以下代码存在问题,我无法使其正常工作。我收到的错误消息是:“无法将类型'(Any)-> Int'的值转换为预期的参数类型'Int'”和“意外...

swift uilabel ibaction
1个回答
0
投票

您全都错了。这是您应该怎么做的完整介绍:

© www.soinside.com 2019 - 2024. All rights reserved.