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

SWIFT: Struggling to add random names within an array

我的应用程序非常基础,首先你输入所有玩家的名字,所有这些名字都存储到一个数组中。第二个最后一个屏幕 pops 有一个句子,一旦你点击一个按钮,句子就会变成数组的第二个元素,再次点击它会转到第三个等等。我能做到的最简单的方法是每次单击它都会删除数组 [0]。

但是在这个数组语句中我已经包含了玩家名字(你在开头输入的那些,但我采用了玩家名字数组的随机元素。所以随机语句工作正常但它不采用随机每次我点击的时候命名。它坚持的第一个随机元素...请帮忙

ps:当我打印(p1)时它会改变但它不会在 screen/app

上改变
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 = ""
  }   
}

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 = ""
  }   
}

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

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

为此,您必须在方法范围之外创建一个变量,并对其递增,直到达到与 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
    }
}

这将帮助您解决问题,假设这正是您在应用程序实现中寻找的内容。

祝你好运,希望对你有所帮助。