-1

I have been stuck on a problem for a couple days now and I'm not sure how to word it, so I haven't had much luck searching for an answer hoping you can help!

I have an array of 20 arrays which is the same as my array of 20 strings, when a button is clicked the array changes to the name of the selected button, I can change it to a string very easily with:

newArray = oldArray[0]

But cannot find a way to insert the string name as the new array name, if anyone has any advice I'd be glad, thanks!

newArray = [oldArray[0]]

Just creates an array with one string in, I have been looking for a function something like:

newArray = Array(named: oldArray [0])

but not sure if it exists.

Roshan
  • 712
  • 4
  • 17
Noah
  • 1
  • 3
  • https://stackoverflow.com/questions/27709723/filter-array-of-anyobject-in-swift – battlmonstr May 13 '18 at 09:44
  • I suppose that the newArray must be of type `[String]`. How does the declaration (type) looks like for oldArray? – battlmonstr May 13 '18 at 09:48
  • You also have flatMap if you want to "flatten" something like `[[String]]` into `[String]` - https://stackoverflow.com/questions/24465281/flatten-a-array-of-arrays-in-swift – battlmonstr May 13 '18 at 09:50
  • So I could flatten the the array into a string? Would that enable me to set the new array name from a string? Edit: thank you for your response! – Noah May 13 '18 at 09:57
  • Can you give some concrete examples of how you declare and use your array? – Cristik May 13 '18 at 09:58
  • i really not understand what you want , i'm sorry can you explain more – Abdelahad Darwish May 13 '18 at 09:59
  • Also note that in Swift you need to use `Dictionary` if you want to have a mapping between a string and some value. `Array` can only map sequential integers (0,1,2...) to values. – battlmonstr May 13 '18 at 10:01
  • I am away from my computer, I shall repost with my example thank you for your time people – Noah May 13 '18 at 10:01
  • 1
    Your question is unclear. What do you mean "new array name?" Array **name**? What is an array name? You talk about having an array of arrays, and then you show code using `oldArray`, and then don't tell us if that is the array of arrays, or show any of your declarations. You need to edit your question to show the way you create oldArray, and you need to explain clearly what you're trying to do. – Duncan C May 13 '18 at 13:38

2 Answers2

0

If you want to insert on some specific index then use insert like this:

var newArr = [String]()
newArr.insert("jogendar", at: 0) /// Removes and returns the element at the specified position.

And if you want to add at the end then use append like this:

var newArr = [String]()
newArr.append("jogendar") ///Adds the elements of a sequence to the end of the array.

If you want to change the name of the button when selected then you don't need to maintain the array, you can set the title of button for states like this:

let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
btn.setTitle("jogendar_defautl", for: UIControlState.normal)
btn.setTitle("jogendar_selected", for: UIControlState.selected)

And when you want to select the button then button name auto change according to selected state:

btn.isSelected = !btn.isSelected
Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26
0

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

Community
  • 1
  • 1
CRD
  • 52,522
  • 5
  • 70
  • 86