1

I have a program in which I have to make an string array equal to another array. The second array needs to be found by its name,

So for example something like this :

let StoreString1 = ["", ""]
let StoreString2 = ["", ""]
let FinalString = GetStringWithName("StoreString" + Number)

in C# its GetComponent("ComponentName");

Thanks for all answers, and sorry for the confusing way I wrote the question, because I didn’t really know hot to put it into words XD.

1
  • Checkout filter function of array Commented Aug 5, 2015 at 14:59

1 Answer 1

1

What you are trying to do can be achieved by using Dictionaries in swift. It appears that you are not familiar with this topic, so will be of no use to just throw out some code, there are many tutorials about this regard.

Just for mentioning one, (that has even images explaining how it works) you can enter here

Happy Learning & Coding! ;-)

UPDATE:

Here's a playground testing the concept: (Feel free to adjust it to your needs)

var dictionary: [String : [ String ]] = ["" : []]
let storeString = "StoreString"

func addUpdateArray(strings: [String], index: Int) {
 let locator = storeString + index.description
 dictionary[locator] = strings
}

func getStringWitNameIndex(index:Int) -> [String]? {
 return dictionary[ storeString + index.description]
}

func addToArray(index:Int, inout destiny: [String]) {
 if let array = getStringWitNameIndex(index) {
     destiny = destiny + array
 }
}
addUpdateArray(["Hello", "World"], 1)
addUpdateArray(["Hello", "Cats"], 3)

var finalArray : [String] = []

addToArray(1,&finalArray)
addToArray(3,&finalArray)

finalArray

In this case, finalArray ends up having: ["Hello", "World", "Hello", "Cats"]

Sign up to request clarification or add additional context in comments.

4 Comments

I looked at it and thanks for the answer but it isn’t really what I meant, or I’m just not getting it. What I wand to do is get the string array "StorageStringX" (X being a variable integer) and than put that array into my final array.
You will need to have all of your strings indexed, you can create a dict builder that appends the corresponding number to the string inside the Dict, ...you know, I'll update my answer, so I can put some code. But you can be looking around about whats the use of Dicts so you understand it better.
@HuRiXD I added a new function and a couple of lines extra to aid you in the finalArray building. Hope it helps you also.
Thanks, Helped a lot XD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.