我正在尝试将随机生成的数据存储到第二个数组中,这样我就可以按一个按钮返回它。此刻我按回去,那里什么都没有。我认为这是由于项目没有正确存储在第二个数组中。我将提供代码供参考。任何帮助将不胜感激!
var randomGeneratedQuotes = Array <String>()
var currentlyShown : String?
var randomQuote: String {
let randomNumber = Int(arc4random_uniform(UInt32(myQuotes.count)))
return myQuotes[randomNumber]
}
var previousRandom: String? {
if currentlyShown == randomQuote {
let previous = randomGeneratedQuotes.index(of: currentlyShown!)! - 1
return randomGeneratedQuotes[previous]
}
return randomGeneratedQuotes.last
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var randomQuote: String {
let randomNumber = Int(arc4random_uniform(UInt32(myQuotes.count)))
return myQuotes[randomNumber]
}
func storePrevious() {
if randomGeneratedQuotes.contains(randomQuote) {return}
randomGeneratedQuotes.append(randomQuote)
}
quotesLabel.text = randomQuote
}
//right hand button tapped
@IBAction func RHSButton(_ sender: Any) {
var randomQuote: String {
let randomNumber = Int(arc4random_uniform(UInt32(myQuotes.count)))
return myQuotes[randomNumber]
}
func storePrevious() {
if randomGeneratedQuotes.contains(randomQuote) {return}
randomGeneratedQuotes.append(randomQuote)
}
quotesLabel.text = randomQuote
}
//left hand button tapped
@IBAction func LHSButton(_ sender: Any) {
func pressedBack() {
currentlyShown = previousRandom
}
quotesLabel.text = previousRandom
}