title: How to fill UISegmentedControl Indexes with String Array Values in Swift
body: <p>I have a UISegmentedControl with 4 indexes that contains quiz answers that are gathered from a string array.  My goal is to fill all 4 indexes with wrong answers, and then randomly replace one of those indexes with the correct answer.  When I run the simulator, the app appears to enter an infinite loop and the view never loads.  When I comment-out the entire second for-loop (which is supposed to fill the indexes with incorrect guesses), the view loads and the segmented control appears with just the correct answer displayed in one segment.  Why is the infinite loop being entered (if that is the case) and how can I modify this code to display the segmented controls as desired?</p>
  
  <pre><code>    func nextQuestion()
      {
          questionNumberLabel.text = String(format: "Question %1$d of %2$d",
              (correctGuesses + 1), numberOfQuestions)
          answerLabel.text = ""
          correctAnswer = allAnimals.removeAtIndex(0)
          animalImageView.image = UIImage(named: correctAnswer) // next animal
  
          // re-enable UISegmentedControls and delete prior segments
          for segmentedControl in segmentedControls
          {
              segmentedControl.enabled = true
              segmentedControl.removeAllSegments()
          }
  
          // place guesses on displayed UISegmentedControls
          allAnimals.shuffle() // shuffle array
          var i = 0        
          for segmentedControl in segmentedControls
          {
              if !segmentedControl.hidden
              {
                  var segmentIndex = 0
  
                  while segmentIndex &lt; 4
                  {
                      if i &lt; allAnimals.count &amp;&amp; correctAnswer != allAnimals[i]
                      {
                          segmentedControl.insertSegmentWithTitle(
                              getStringFromFile(allAnimals[i]),
                              atIndex: segmentIndex, animated: false)
                          ++segmentIndex
                      }
                      ++i
                  }
              }
          }
  
          // pick random segment and replace with correct answer        
          let randomIndexInRow = Int(arc4random_uniform(UInt32(4)))
          segmentedControls[0].removeSegmentAtIndex(
              randomIndexInRow, animated: false)
          segmentedControls[0].insertSegmentWithTitle(
              getStringFromFile(correctAnswer),
              atIndex: randomIndexInRow, animated: false)
      }
  </code></pre>