简体   繁体   English

如何将独立值绑定到数组中的字符串(Swift)?

[英]How can I tie independent values to strings in an array (Swift)?

So I'm practicing coding and I've set up an array of strings.所以我正在练习编码,我已经设置了一个字符串数组。 What I want to happen is to have a function randomly select a string and if a certain string is selected, it would print out one message, and if any of the other strings are selected, it would print out a different one.我想要发生的是有一个 function 随机 select 一个字符串,如果选择了某个字符串,它将打印出一条消息,如果选择了任何其他字符串,它将打印出不同的消息。 I think setting up the messages could be done w/an if-else function, but I'm stuck on the earlier step of figuring out how to identify which string is called.我认为设置消息可以使用 if-else function 来完成,但我被困在弄清楚如何识别调用哪个字符串的较早步骤上。

I did some searching and found this thread Get Random String from an Array and I copied the randomization code from it.我做了一些搜索,发现这个线程Get Random String from an Array并从中复制了随机化代码。 I'm a bit overwhelmed with where to go from there.我对从那里到 go 的位置有点不知所措。

What type of function/identifier etc would I need to set up so that the strings are uniquely identified?我需要设置什么类型的函数/标识符等以便唯一标识字符串?

class ViewController: UIViewController {
    
    // Make collection of games and if Monsters is called, print "Take a break." If a diff string is called, print "Do your homework."
    @IBAction func random(_ sender: UIButton) {
        let games = ["Cards Against Humanity", "Mario Kart", "Halo", "Pixeljunk Monsters"]
        let randomNumber = Int(arc4random_uniform(UInt32(games.count)))
        _ = games[randomNumber]
    }
} // end of class

Your expectation is not very clear from your question, but I guess you are beginner and need a help with processing randomly selected string.从你的问题中你的期望不是很清楚,但我想你是初学者,需要帮助来处理随机选择的字符串。

@IBAction func random(_ sender: UIButton) {
            let games = ["Cards Against Humanity", "Mario Kart", "Halo", "Pixeljunk Monsters"]
            let randomNumber = Int(arc4random_uniform(UInt32(games.count)))
            let randomlyPickedString = games[randomNumber]
            if randomlyPickedString == "Cards Against Humanity" {
                print("Print anything you want about Cards Against Humanity")
            }
            else if randomlyPickedString == "Mario Kart" {
                print("Print anything you want about Mario Kart")
            }
            else if randomlyPickedString == "Halo" {
                print("Print anything you want about Halo")
            }
            else {
                print("Print anything you want about Pixeljunk Monsters")
            }
        }

If its a finite set of values in array, you can even decide to use switch如果它是数组中的一组有限值,您甚至可以决定使用switch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 2:如何将动态数目的字符串分配给字符串数组? - Swift 2: How can I assign a dynamic number of strings to an array of Strings? 如何从字符串数组创建枚举值数组 - How can I create an array of enum values from an array of Strings 如何将数组切片绑定到原始数​​组,以便对两者进行所有更改? - How can I tie an array slice to the original array so all changes that are made to one are made to both? Swift:如何对数组中结构的子值使用 UISearchResultsUpdating? - Swift: How can I use UISearchResultsUpdating on child values of structs in an array? 如何在 Swift 中对具有关联值的枚举数组进行排序? - How can I sort an array of enums with associated values in Swift? 如何快速解析字符串数组? - How to parse array of strings in swift? 如何在数组中找到第二个最常见的字符,在平局的情况下,从左侧出现的第二个最常见的字符 - How can I find the second most common character in an array, where in the case of a tie, the second most frequent characters appearing from the left 如何搜索字符串数组并迅速返回特定值? - How do I search an array of strings and return a specific value in swift? 在Swift中,如何将数组中的数字字符串转换为Doubles - In Swift, how do I convert number strings in an array to Doubles 我如何在 Ada 中定义一个字符串数组? - How can I define an Array of Strings in Ada?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM