swiftではarcNrandom_uniform(UInt32(hoge.count))でランダムに数値を生成します。
Tips
command + option + 0: utility 非表示
launch screen -> main.storyboardに変更可能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import UIKit class ViewController: UIViewController { @IBOutlet weak var myLabel: UILabel! @IBAction func getOmikuji(sender: AnyObject) { let results = [ "大吉", "吉", "中吉", "凶", "大凶" ] // arc4random_uniform(n + 1) let random = arc4random_uniform(UInt32(results.count)) if random == 0 { self.myLabel.textColor = UIColor.redColor() } else { self.myLabel.textColor = UIColor.blackColor() } self.myLabel.text = results[Int(random)] } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |