代码之家  ›  专栏  ›  技术社区  ›  Neillbar

SWIFT:在数组中添加随机名称很困难

  •  1
  • Neillbar  · 技术社区  · 6 年前

    我的应用程序非常简单,首先你输入所有球员的名字,所有这些名字都存储到一个数组中。第二步,最后一个屏幕弹出一个句子,单击按钮后,句子将变为数组的第二个元素,再次单击将变为第三个元素,以此类推。。我能做的最简单的方法是每次单击它时删除数组[0]。

    但是在这个数组句子中,我已经包含了玩家的名字(你在开头键入的那些名字,但我从玩家名字数组中提取了一个随机元素。因此,随机语句工作正常,但它不是每次单击时都提取一个随机名称。它提取的第一个随机元素会粘住…请帮助

    ps:当我打印(p1)时,它会改变,但在屏幕/应用程序上不会改变

    import UIKit
    
    var q = ["Hello " + p1,"wie is die beste ? Malcolm is !!!","Hoe Gaan Dit " + p1 + " en met jou ook " + p2,"Gaan Baie goed dankie " + p1,"Wat Maak jy " + p2,"Ek sit maar hierso " + p2,"We are getting somewhere " + p1,"blah blah blah hoe gaan daai song van eminem alweer " + p1,"EK vorder met apps maak elke dag hehe " + p2,""]
    
    var p1 = ""
    var p2 = ""
    var random = [String]()
    
    var pc1 = 0
    var pc2 = 0
    
    /////////////////////////
    
    @IBAction func next(_ sender: Any) {
      //chosing a random player
    
      pc1 = Int(arc4random_uniform(UInt32(players.count)))
      pc2 = Int(arc4random_uniform(UInt32(players.count)))
    
      if pc1 == pc2{
         pc1 = Int(arc4random_uniform(UInt32(players.count)))
         pc2 = Int(arc4random_uniform(UInt32(players.count)))
      }
    
      p1 = players[pc1]
      p2 = players[pc2]
    
      if q[0] != "" {
        dis.text = q[0]
        q.remove(at: 0)
      } else {
        dis.text = ""
      }   
    }
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   J Arango    6 年前

    问题在于连接。您正在创建一个字符串数组,其中包含串联,当从操作内部调用该数组时,不会使用该数组的串联。

    最好的解决方案是在action方法的范围内创建引号数组,并在索引上递增,直到达到数组的总计数。

    为此,您必须在方法的作用域之外创建一个变量,并在其上递增,直到达到与q的计数相同的数字。一旦达到相同的计数,就会将其重置为0,然后重新开始该过程。

    示例:

    var p1 = ""
    var p2 = ""
    var random = [String]()
    
    var pc1 = 0
    var pc2 = 0
    var index = 0
    
    
    /////////////////////////
    
    @IBAction func next(_ sender: Any) {
       //chosing a random player
    
     pc1 = Int(arc4random_uniform(UInt32(players.count)))
     pc2 = Int(arc4random_uniform(UInt32(players.count)))
    
     if pc1 == pc2{
         pc1 = Int(arc4random_uniform(UInt32(players.count)))
         pc2 = Int(arc4random_uniform(UInt32(players.count)))
     }
    
     p1 = players[pc1]
     p2 = players[pc2]
    
     // Array of quotes is now inside the scope of the method.
     var q = ["Hello " + p1,"wie is die beste ? Malcolm is !!!","Hoe Gaan Dit " + p1 + " en met jou ook " + p2,"Gaan Baie goed dankie " + p1,"Wat Maak jy " + p2,"Ek sit maar hierso " + p2,"We are getting somewhere " + p1,"blah blah blah hoe gaan daai song van eminem alweer " + p1,"EK vorder met apps maak elke dag hehe " + p2,""]
    
        // Using the quote
        dis.text = q[index]
    
        // Incrementing index
        index += 1
    
        // Keeping track of the index's count
        // and reseting it back to 0
        if index == q.count {
            index = 0
        }
    }
    

    这将帮助您解决问题,前提是这是您在应用程序实现中需要的。

    祝你好运,希望这有帮助。

        2
  •  0
  •   Will Richardson    6 年前

    中的文本 q 如果它是ViewController的属性,则仅设置一次。如果要根据用户名进行更新,则需要在选择玩家名称后创建:

    import UIKit
    
    var currentIndex = 0
    var p1 = ""
    var p2 = ""
    var random = [String]()
    
    var pc1 = 0
    var pc2 = 0
    
    /////////////////////////
    
    @IBAction func next(_ sender: Any) {
      //chosing a random player
    
      pc1 = Int(arc4random_uniform(UInt32(players.count)))
      pc2 = Int(arc4random_uniform(UInt32(players.count)))
    
      if pc1 == pc2{
         pc1 = Int(arc4random_uniform(UInt32(players.count)))
         pc2 = Int(arc4random_uniform(UInt32(players.count)))
      }
    
      p1 = players[pc1]
      p2 = players[pc2]
    
      var q = ["Hello " + p1,"wie is die beste ? Malcolm is !!!","Hoe Gaan Dit " + p1 + " en met jou ook " + p2,"Gaan Baie goed dankie " + p1,"Wat Maak jy " + p2,"Ek sit maar hierso " + p2,"We are getting somewhere " + p1,"blah blah blah hoe gaan daai song van eminem alweer " + p1,"EK vorder met apps maak elke dag hehe " + p2,""]
    
      if q[0] != "" {
        dis.text = q[currentIndex]
        currentIndex += 1
      } else {
        dis.text = ""
      }   
    }