2016-04-06 6 views
0

Wie mache ich arc4random wählen Sie 4 verschiedene Namen ohne Antwort, wenn ich 5 Namen im Textfeld schreiben?wie man arc4random wählt 4 verschiedene Namen in swift

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

Entfernen Sie das Element aus dem Array, wenn Sie es abgeholt, und dann den „maximalen“ ändert Wert des Zufalls. – Larme

+0

Warum ist der Etikettenausgang schwach? – iMuzahid

+0

Bitte umformulieren Sie Ihre Frage, weil es unverständlich ist –

Antwort

0

Wie ich verstanden habe, wollen Sie aus einer Liste von fünf Namen, zufällig 4 wählen und (zufällig) koppeln diese in zwei Paare.

/* 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 */ 

zu Ihrem Beispiel Beworben:

// ... 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

ohhh Mann, du bist so gut, vielen Dank, es ist endlich Arbeit <3 –

+0

@ X.BOZO Glücklich zu helfen. Stellen Sie nur sicher, dass Ihr 'UILabel' 'lbl' so eingestellt ist, dass mehrere Zeilen angezeigt werden, oder das zweite Paar wird abgeschnitten. (z.B. 'lbl.numberOfLines = 2'). – dfri

+0

ok danke, mein Herr <3 –

Verwandte Themen