2014-09-20 47 views
0

我已经完成了在树屋中用Swift课程构建一个简单的iPhone应用程序,现在我想添加一个功能。我想阻止连续显示相同的背景颜色和相同的有趣事实。我一直在努力,但我似乎无法弄清楚正确的方法。我尝试在FactBook.swift和ColorWheel.swift中存储一个randomIndex变量来保存生成的随机数以获得随机事实/颜色。然后我使用do-while循环将该随机数与变量进行比较,如果匹配,则生成另一个随机数。但是,我无法在循环中获得正确的范围。如何防止在Swift中连续显示相同的随机颜色和随机事实?

这里是我的代码,我对项目迄今:

ViewController.swift

import UIKit 

    class ViewController: UIViewController { 

    @IBOutlet weak var funFactLabel: UILabel! 
    @IBOutlet weak var funFactButton: UIButton! 

    let factBook = FactBook() 
    let colorWheel = ColorWheel() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     funFactLabel.text = factBook.randomFact() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func showFunFact() { 
     var randomColor = colorWheel.randomColor() 
     view.backgroundColor = randomColor 
     funFactButton.tintColor = randomColor 
     funFactLabel.text = factBook.randomFact() 
    } 

} 

FactBook.swift

import Foundation 

struct FactBook { 

    var randomIndex = 0 

    let factsArray = [ 
     "Ants stretch when they wake up in the morning.", 
     "Ostritches can run faster than horses.", 
     "Olympic gold medals are actually mostly made of silver.", 
     "You are born with 300 bones; by the time you are an adult you will have 206.", 
     "It takes about 8 minutes for light from the Sun to reach the Earth.", 
     "Some bamboo plants can grow almost a meter in just one day.", 
     "Some penguins can leap 2-3 meters out of the water.", 
     "The state of Florida is bigger than England.", 
     "On average, it takes 66 days to form a new habit.", 
     "Mammoths still walked the Earth when the Great Pyramid was being built." 
    ] 

    func randomFact() -> String { 
     var unsignedArrayCount = UInt32(factsArray.count) 
     var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount) 
     var randomNumber = Int(unsignedRandomNumber) 

     // verify value of randomNumber 
     println(randomNumber) 

     return factsArray[randomNumber] 
    } 

} 

ColorWheel.swift

import Foundation 
import UIKit 

struct ColorWheel { 
    let colorsArray = [ 
     UIColor(red: 90/255.0, green: 187/255.0, blue: 181/255.0, alpha: 1.0), //teal color 
     UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0), //yellow color 
     UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0), //red color 
     UIColor(red: 239/255.0, green: 130/255.0, blue: 100/255.0, alpha: 1.0), //orange color 
     UIColor(red: 77/255.0, green: 75/255.0, blue: 82/255.0, alpha: 1.0), //dark color 
     UIColor(red: 105/255.0, green: 94/255.0, blue: 133/255.0, alpha: 1.0), //purple color 
     UIColor(red: 85/255.0, green: 176/255.0, blue: 112/255.0, alpha: 1.0), //green color 
    ] 

    func randomColor() -> UIColor { 
     var unsignedArrayCount = UInt32(colorsArray.count) 
     var unsignedRandomNumber = arc4random_uniform(unsignedArrayCount) 
     var randomNumber = Int(unsignedRandomNumber) 

     return colorsArray[randomNumber] 
    } 
} 

回答

2

使可变c选择两个数组并将这些可变数组用作工作数组。在这些数组中选择一个随机元素后,删除该成员。当计数为零时,如果您想继续操作,请创建一个新副本。