SWIFT:努力在数组中添加随机名称

时间:2018-03-28 22:36:48

标签: arrays swift xcode swift3 swift4

我的应用程序非常基本,首先输入所有播放器名称,所有这些名称都存储到数组中。其次,最后一个屏幕弹出一个句子,一旦你点击一个按钮,句子就会变成数组的第二个元素,再次点击它就会转到第三个等等......我最简单的方法就是每次点击它删除数组[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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

最佳解决方案是在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
    }
}

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

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