2

I am trying to change my string value in an array after shuffling another array, how am i to do this?

Example:

var array1 = [1,2,3,4,5,6]

var stringarray = ["\\(array1[0]) = 1") 

array1.shuffle()

print(stringarray)

How am i to change the original stringarray value to the new shuffled value?

Thank you

The task:

    @IBAction func nextQuestion(_ sender: Any) {

    if levelSelected == 1 {
        questionLabel.text = standardRules.randomElement()
    }
    players.shuffle()
    print(players)

standardRules has a string value that takes the value of players[0]

Essentially what i am trying to do is this:

I am trying to grab 2 random values that are not the same in a string like this

    var players = ["Jack, John, Michael, Peter"]
    var playersArray = ["\(players.randomElement) and \(players.randomElement) has to battle")

How am i to do this, so it grabs 2 different values?

Thuesen
  • 29
  • 4
  • What are you trying to achieve? Maybe it worth to show us the exact task? – Andrei Chevozerov Oct 27 '18 at 19:59
  • What do you need `stringArray` to show? Also change the 2nd line in the code you posted to a more accurate example. – Rakesha Shastri Oct 27 '18 at 20:03
  • So what should be changed in the end after players.shuffle()? The text on questionLabel? – Andrei Chevozerov Oct 27 '18 at 20:17
  • Yes, questionLabel.text should be the new value from the element in standardRules which is an array, with value like this: standardRules = ["\(players[0]) has to do this"] Then i want a new value after shuffling the players array – Thuesen Oct 27 '18 at 20:23
  • As already mentioned in your [previous question](https://stackoverflow.com/questions/53026461/grab-2-randomelements-that-are-not-the-same) consider that `players` contains **one** item, a string with some names comma separated. – vadian Oct 29 '18 at 09:24
  • That's a mistake though, it is supposed to be var players = ["Jack", "John", "Michael", "Peter"] – Thuesen Oct 29 '18 at 12:50

3 Answers3

0

Looks like you're missing some core concepts of the Swift language. When you create your stringarray it makes a call of array1.description and stores the result into an array. From that point any modifications of the original array will not change anything in stringarray.

So if you want to pick two different players from an array you need to do something like that:

let index1 = Int.random(in: (0 ..< players.count))
var index2: Int
repeat {
    index2 = Int.random(in: (0 ..< players.count))
} while index1 == index2
let matchText = "\(players[index1]) and \(players[index2] has to battle)"
Andrei Chevozerov
  • 1,029
  • 1
  • 10
  • 24
0

I would try replacing:

var stringarray = ["\\(array1[0]) = 1") 

array1.shuffle()

with:

var stringarray = array1.shuffle()
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Scott R.
  • 36
  • 7
0

You could make it very easy by this code :

var players = ["Jack", "John", "Michael", "Peter"]

// get the first random element
var random1 = players.randomElement()

//delete that element so you can't duplicate it 
players = players.filter{$0 != random1}

//get your second radom element
var random2 = players.randomElement()

//add your element again
players.append(random1!)
Daniel Larsson
  • 527
  • 2
  • 6
  • 23