2017-06-13 48 views
-1

超过100分以上的问题,我想开发的ios测验应用程序。我看到它使用的开关情况。现在,我有超过数百questions.How,我可以实现它,我需要我的问题是随机的教程。如何生成randon问答题时,我们在IOS

+0

创建一个文本文件,并把你的100个问题的JSON format.it将很容易Modify.Read文件显示您的问题使用1 100 –

+0

之间对这个问题最终可能会被关闭,因为它不符合SO指引随机数。这里是他们的指导下,这可能会帮助你提高它:stackoverflow.com/help/how-to-ask - 代码小样本显示你已经尝试可能会提高的问题,增加您更好的回答的机会是什么。 –

回答

1
let randomIndex = Int(arc4random_uniform(UInt32(questionsArray.count))) 
print(questionsArray[randomIndex]) 

使用上面的代码中挑选随机问题从一个数组

0

可以使用shuffle function这里洗你的问题。只是作为

var nums = [1, 2, 3, 4, 5] 
nums.shuffle() // this will mutate and changed to shuffled array [5, 3, 1, 2, 4] 

使用的精确延伸低于:

extension MutableCollection where Indices.Iterator.Element == Index { 
    /// Shuffles the contents of this collection. 
    mutating func shuffle() { 
     let c = count 
     guard c > 1 else { return } 

     for (firstUnshuffled , unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) { 
      let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount))) 
      guard d != 0 else { continue } 
      let i = index(firstUnshuffled, offsetBy: d) 
      swap(&self[firstUnshuffled], &self[i]) 
     } 
    } 
}