Problem with matching arrays and indexes

I have a very confusing situation that I can't figure out. I'm not quite fluid in swift so please bear with me if this is a rookie mistake.

So let's say I have a variable x

let number = Int.random(in: 0 ... 5)
x = number
let number2 = Int.random(in: 0 ... 5)
y = number2

ok, so establishing that x is a random integer from 0 to 5 if I'm not correct, which means it's 6 values

Also, we have this array

let arrayValues = ["apples", "bannanas", "cherry", "date", "eggplant", "dorito"]

Here we can begin with the situation, I will include some of the steps I have attempted.

So I want to have x, let's say in this case the computer chooses a 3, and y is 5.

Keep in mind that every time the program runs, it will provide a different number.

What I want to do, is to match those x and y values with the indexes of the array "arrayValue".

In this example, x = 3 and y = 5

therefore if we compare the index of the array "arrayValues", with the x and y that would be

"date", (the index would be 3 which is equal to x )and "dorito" (the index is 5, which is equal to y).

However, here's the problem, how do I do this? Because the values are changing every time, I also can't use arrayValue.contain to find the value. due to the fact, the numbers aren't strings. As well as the fact if we convert the numbers into strings it would be pointless again, because it would be searching for "x", and not searching for the actual value of x.

Is there a code to fix this?

Thanks in advance.

Accepted Reply

You cannot set a string to a var (x or y) defined as Int.


What you have simply to do:

- generate a random number x

var x: Int = Int.random(in: 0 ... 5)


- use this x to get the string value from array

let arrayValues = ["apples", "bannanas", "cherry", "date", "eggplant", "dorito"]     
var xString = arrayValues[x]


Now on, use xString and not x to refer to the selected object.

Note: no need to use enumerated here, arrays are ordered, that's enough here.


A more direct way to do it is to call random directly on the array:


let arrayValues = ["apples", "bannanas", "cherry", "date", "eggplant", "dorito"]
let x = arrayValues.randomElement() ?? ""
print(x)

Replies

Sorry, but I cannot understand what you want to do.

Say the values, please explain the values of what? About it, what are those all its here and there?


Maybe showing more code, even if it does not work as you expect, would help readers understand what you want to do.

I apologize, it is confusing. I don't know what to do so there is no code yet. My previous attempts are snippets and are not whole. I'm looking if there is a function to do this process below before I start coding again.

The values are values of the variables x and y, in this specific case the values are 3 and 5, however, the values of x and y will change every loop.

As well as the fact if we convert the numbers into strings it would be pointless again, because it would be searching for "x", and not searching for the actual value of x.

What I was trying to say here is simply I tried the function arrayValue.contains

and in order to do that .contains function, I would need to string x, which would make the .contains function search for "x", instead of the actual value of x, in this example is 3.

if arrayValue.contains("x"){}

Sorry if it's not clear, I'll do my best, since I am confused myself.

You should not access with "x"

if arrayValue.contains("x"){}


but with

arrayValue[x]


but testing

if arrayValue.contains(arrayValue[x])

will always return true, so nuseless.


What you want to test is not clear.

Can you show more examples?


Let's say we have:

let arrayValues = ["apples", "bannanas", "cherry", "date", "eggplant", "dorito"]   
let x = 1
let y = 3

Then, what is the desired output? Some boolean value `true` or `false`? An array? Or some integers?


Please show some examples, input(s) - output(s). You should better omit intermediate values and just show the final result.

So the computer assigns a random integer from 0-5 and assigns it to x and y

input:

x = 1

y = 2

after the program runs whatever function it is to calculate the indexes of the array and match it up with x = 1 and y = 2

so the array =

let arrayValues = ["apples", "bannanas", "cherry", "date", "eggplant", "dorito"]  

Following that the computer would enumerate the objects

let arrayValues = [0: "apples", 1:"bannanas", 2:"cherry", 3:"date", 4: "eggplant", 5:"dorito"]  

here is the output:

the computer would look at the enumerated objects and then change the input variables into

x = "bannanas"

and

y = "cherry"

In the end, basically replacing the random generated numeral and replacing it with the array. The preoblem is, I have to have x and y generate random integers, I understand that it is possible to randomly select an array value, it is the same concept, and 1. In this specific case I cannot use that function, 2. I want to see if there is other functions that can do the same thing.

Here is another example

input:

x= 4

y = 0

output:

x = "eggplant"

y = "apples"


Here is the problem

let arrayValues = ["apples", "bannanas", "cherry", "date", "eggplant", "dorito"]

As you can see in this array there is no x, and the computer will recognise it, and throw up an error

I am looking for the value of x, and not "x" itself.

What I am testing is not a boolen function. Check my reply to OOper below

You cannot set a string to a var (x or y) defined as Int.


What you have simply to do:

- generate a random number x

var x: Int = Int.random(in: 0 ... 5)


- use this x to get the string value from array

let arrayValues = ["apples", "bannanas", "cherry", "date", "eggplant", "dorito"]     
var xString = arrayValues[x]


Now on, use xString and not x to refer to the selected object.

Note: no need to use enumerated here, arrays are ordered, that's enough here.


A more direct way to do it is to call random directly on the array:


let arrayValues = ["apples", "bannanas", "cherry", "date", "eggplant", "dorito"]
let x = arrayValues.randomElement() ?? ""
print(x)