0

Im very new to Xcode and swipe , any help is much appreciated. I have created a random generator for a words list on the top half of the screen(the top label, this is included in the code). How do i adapt this code so that i can use a different list of words on the bottom half of the screen ( the bottom label is not attatched to any code)? but for the same click button to randomise both list of words ? The layout of the screen

import UIKit

class ViewController: UIViewController { // put the labels and the buttons

@IBOutlet var InspirationalThought: UILabel!

@IBOutlet var Click: UIButton!

//what the label is going to show
var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle", "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion",
    "Ancient Greek", "Philosophers", "Fairy Tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air",
          "Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music Notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior",
          "Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"]

var whichQuotestoChoose = 0

// what the button is going to show

var ButtonText = ["Inspiration", "Tell me more", "more inspirational"]
var whichButtonTexttoChoose = 0





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


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()


}



@IBAction func ClickButtonClicked(sender: UIButton) {
    chooseAQuote()
    chooseTextonButton()

}

func chooseAQuote(){
    showTheQuote()
    whichQuotestoChoose = Int(arc4random_uniform (84))

}
func showTheQuote(){
    InspirationalThought.text = "\(quotes[whichQuotestoChoose])"

}
func chooseTextonButton(){
    Click.setTitle("\(ButtonText[whichButtonTexttoChoose])", forState: UIControlState.Normal)


}

}

1 Answers1

1

Use extension it is global. you can use shuffle the array and print the quote. you can declare quotes array global so you can access it in any view controller.

   var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle", "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion",
              "Ancient Greek", "Philosophers", "Fairy Tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air",
              "Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music Notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior",
              "Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"]

extension CollectionType {
    /// Return a copy of `self` with its elements shuffled
    func shuffle() -> [Generator.Element] {
        var list = Array(self)
        list.shuffleInPlace()
        return list
    }
}

extension MutableCollectionType where Index == Int {
    /// Shuffle the elements of `self` in-place.
    mutating func shuffleInPlace() {
        // empty and single-element collections don't shuffle
        if count < 2 { return }

        for i in 0..<count - 1 {
            let j = Int(arc4random_uniform(UInt32(count - i))) + i
            guard i != j else { continue }
            swap(&self[i], &self[j])
        }
    }
}

quotes = quotes.shuffle()

print(quotes[0])  // Print random quote

OUTPUT

Shuffle extension are not written by me. Credits Nate Cook : https://stackoverflow.com/a/24029847/2803960

Community
  • 1
  • 1
O-mkar
  • 5,430
  • 8
  • 37
  • 61