ios - 如何跟踪数组中的哪些项目已经存在?

标签 ios swift

我有一个生成随机问题的测验应用程序。目前由于随机化,您有时可以多次回答同一个问题。我有一系列问题,我使用 arc4Random 方法在屏幕上生成一个随机问题。

我如何才能让 swift 识别出一个问题已经存在并跳过它?我必须做一个 if 声明吗?如果是这样,我将如何编码?

var currentQuestion = Int(arc4random_uniform(31) + 1)
var rightAnswerPlacement:UInt32 = 0
var seconds = 30
var timer = Timer()
var score = 0
if (sender.tag == Int(rightAnswerPlacement)) {
    score += 1
    scoreLabel.text = "\(score)"
    seconds = 100
    currentQuestion = Int(arc4random_uniform(32) + 1)
} else {
    timer.invalidate()
    end()
}

最佳答案

var currentQuestion = Int(arc4random_uniform(36) + 1)
var rightAnswerPlacement:UInt32 = 0
var seconds = 30
var timer = Timer()
var score = 0
var asked = [Int]()

var recordData:String!

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!

@IBOutlet weak var buttonOne: UIButton!
@IBOutlet weak var buttonTwo: UIButton!
@IBOutlet weak var buttonThree: UIButton!

@IBAction func answerButtons(_ sender: AnyObject) {

    if (sender.tag == Int(rightAnswerPlacement)) {

        score += 1
        scoreLabel.text = "\(score)"
        seconds = 100
        currentQuestion = Int(arc4random_uniform(36) + 1)

       while asked.contains(currentQuestion) {

            currentQuestion = Int(arc4random_uniform(36) + 1)
        }
        asked.append(currentQuestion)
        print(asked)

        if asked.count == questions.count {
            currentQuestion = 0
          return
        }

关于ios - 如何跟踪数组中的哪些项目已经存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54410125/

相关文章:

ios - Swift 中的延迟/ sleep 不起作用

ios - 在 IQKeyboardManager 中始终保持 View 在顶部(不要使用键盘滚动)

ios - UITableView 重新加载问题

ios - 为什么我的 Vapor 查询总是失败?

ios - 上传图像/视频到 FTP Swift 3.0

ios - iOS检测短信发送失败

ios - 我可以将 SKAction runBlock 与 SKNode - runAction :completion:? 函数结合起来吗?

ios - 转换二维数组

iphone - 如何检测 UITableView 的拖动结束事件?

ios - Swift 2.0 元组模式元素标签必须是 '_'