This is not an answer as such, you do not provide enough detail for that, but let's see if we can help you onto the right track. You wrote:
I have been looking for a function something like:
newArray = Array(named: oldArray [0])
This does not exist because arrays (and other values) do not have names. You appear to be confusing variable names and values, they are very different things. For example, the expression:
[0, 1, 8, 27, 64]
produces an array value containing five elements, it has no name it is just a value. The value could be assigned to a variable:
var cubes = [0, 1, 8, 27, 64]
Now we have a variable with the name cubes
whose current value is the array. The above statement has both declared a variable, cubes
, given it a type, [Int]
, and assigned it an initial value, [0, 1, 8, 27, 64]
. It has not named the value, the value can be copied into another variable:
var anotherVariable = cubes
After the above you have two variables, cubes
and anotherVariable
, each which currently has the same value. Values are not shared, you can alter one of the values:
anotherVariable[0] = 42
and now anotherVariable
contains the value [42, 1, 8, 27, 64]
while cubes
still contains the value [0, 1, 8, 27, 64]
.
OK, back to your question, you wrote:
I have an array of 20 arrays
which is the same as my array of 20 strings
...
What you appear to be describing is something along the lines of:
var myArrays = [ [0, 1, 4, 9, 16], [0, 1, 8, 27, 64] ]
var myNames = [ "squares", "cubes" ]
and you have buttons with labels squares
and cubes
. What you appear to be attempting is to go from a click on a button to an element of myArrays
via the myNames
array - which is where you "naming" of values is coming from.
To map a "name" to a "array" you should use a dictionary, which is a collection keys, your names, to values, your arrays:
let myCollection = [ "squares":[0, 1, 4, 9, 16], "cubes":[0, 1, 8, 27, 64] ]
This declares a constant (the use of let
rather than var
) dictionary whose keys are strings and values arrays. So you effectively have a collection of "named arrays".
You can now select an array for this collection by name using a statement along the lines of:
var currentArray = myCollection[currentButtonTitle]
where currentButtonTitle
has previously been set to the title of the button you clicked.
HTH