2016-04-06 7 views
0

テキストフィールドに5つの名前を書くとき、arc4randomを4つの異なる名前を返すようにするにはどうすればよいですか?私が理解してきたようにarc4randomをスピーディで4種類の名前を選択する方法

import UIKit 

class ViewController: UIViewController, UIPickerViewDelegate { 

    var array = [String]() 

    @IBOutlet var textfi1: UITextField! 
    @IBOutlet var textfi2: UITextField! 
    @IBOutlet var textfi3: UITextField! 
    @IBOutlet var textfi4: UITextField! 
    @IBOutlet var textfi5: UITextField! 


    @IBOutlet weak var lbl: UILabel! 



    override func viewDidLoad() { 
     super.viewDidLoad() 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    @IBAction func button(sender: AnyObject) { 

      numberFive() 

    } 

    func numberFive() { 

     array = [ textfi1.text! , textfi2.text! , textfi3.text! , textfi4.text! , textfi5.text! ] 

     let randomIndex = Int(arc4random_uniform(UInt32(5))) 

     lbl.text! = "\(array[randomIndex]) with \(array[randomIndex]) \n \(array[randomIndex]) with \(array[randomIndex])" 

     lbl.numberOfLines = 0 

     }  

    } 
+1

は、あなたがそれを選んだ後は、配列から項目を削除した後、ランダムの「最大」の値を変更します。 – Larme

+0

あなたのラベルコンセントはなぜ弱いのですか? – iMuzahid

+0

理解できないので質問を言い換えてください –

答えて

0

、あなたは、5名のリストから、ランダムに4を選択し、(ランダム)2ペアにこれらをペアリングします。

あなたの例に適用
/* say these are the names from the .text property 
    of your UITextField instances */ 
var names = ["David", "Lisa", "Greg", "Hannah", "Richard"] 

/* create an array of tuples to hold the pairs */ 
var pairs : [(String, String)] = [] 

/* randomly assign 4 out of 5 names into two pair containers */ 
let numPairs = names.count/2 // Int division => rounds down by simply dropping decimal value 
for _ in 0..<numPairs { 
    var newPair : (String, String) 
    newPair.0 = names.removeAtIndex(Int(arc4random_uniform(UInt32(names.count)))) 
    newPair.1 = names.removeAtIndex(Int(arc4random_uniform(UInt32(names.count)))) 
    pairs.append(newPair) 
} 

/* random result */ 
pairs.forEach { print("\($0.0) with \($0.1)") } 
/* Hannah with Lisa 
    Greg with Richard */ 

// ... in your ViewController class 

func divideIntoPairs() { 

    if let name1 = textfi1.text, name2 = textfi2.text, 
     name3 = textfi3.text, name4 = textfi4.text, 
     name5 = textfi5.text { 

     var names = [name1, name2, name3, name4, name5] 

     var pairs : [(String, String)] = [] 

     let numPairs = names.count/2 
     for _ in 0..<numPairs { 
      var newPair : (String, String) 
      newPair.0 = names.removeAtIndex(Int(arc4random_uniform(UInt32(names.count)))) 
      newPair.1 = names.removeAtIndex(Int(arc4random_uniform(UInt32(names.count)))) 
      pairs.append(newPair) 

     } 
     pairs.forEach { print("\($0.0) with \($0.1)") } 
     /* 'SomeName' with 'AnotherName' 
      'EvenAnotherName' with 'AFinalOtherName' */ 

     // or, to set this as a single String to UILabel: text property 
     lbl.text = pairs.map { "\($0.0) with \($0.1)" }.joinWithSeparator("\n") 
    } 
} 
+0

オハイオ州の人あなたはとても良いです、ありがとう、それは最終的に仕事<3 –

+0

@ X.BOZO助けて幸いです。あなたの 'UILabel' 'lbl'が複数の行を表示するように設定されていることを確認してください。そうでない場合、2番目のペアリングは切り捨てられます。 (例えば、「lbl.numberOfLines = 2」)。 – dfri

+0

ありがとうございます<3 –

関連する問題