0

My end goal is to pass the desired array to a function. The problem is the array name depends on the situation. It may be 1 of 30 names.

I have about 30 arrays all named "default_SomeName". Now the previous VC passes the SomeName value. Based on that value, my goal is to pass one of the 30 arrays to a function. Yet, dynamic references are apparently not easy.

I've tried:

var someName: String! //Passed from presenting VC
let desiredArray = "default_" + someName
myFunction(desiredArray)

But I get:

Cannot convert value of type 'String' to expected argument type [String]

Anyone know how to do this?

3
  • Due to the static nature of Swift, you can only achieve something like this by leaving Swift and going to Obj-C and Foundation methods, using KVO methods of NSObject. Since you're coding in Swift, however, I would advice against this. Possibly change the approach instead; could the "passing VC" pass a simple integer reference instead? Say an Int between 1 and 30 that corresponds to the index in an array of string arrays, [[String]] (which holds your 30 or so String arrays). Commented Feb 13, 2016 at 23:46
  • 1
    You could put the arrays in a dictionary and then you can access the required array by 'name' (key) Commented Feb 13, 2016 at 23:51
  • @Paulw11 - That's interesting... so I make the someName the key and the arrays the thing return? I've never used a dictionary before, never mind for something like that. If you think it'll work, can you post it as an answer? Commented Feb 14, 2016 at 0:13

2 Answers 2

2

You can use a dictionary to associate each dictionary with it's name. Dictionaries are explained in the Apple Swift book, but generally you would do something like this:

var dictionary=[String:[Int]]()

dictionary["default_1234"]=[1,2,3,4]
dictionary["default_5678"]=[5,6,7,8]

let suffix="1234"

print(dictionary["default_\(suffix)"])

In this case I have used an array of Int, but you can have any array type

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

4 Comments

I started reading about them after you mentioned it in the comment. So you're suggested I declare an empty one but set the types. Then, one by one, add a key and the array it should reference. Ultimately, it will allow me to pass the desired array like this?: myFunction(dictionary["SomeKeyName"])
That's right. It still seems rather clunky, but without knowing more about what you are trying to achieve I can't suggest anything else.
var dictionary = [String : [String]]() dictionary["Quote"] = defaultNames_Quote - This code is just getting the error 'Expected Declaration' - Maybe I should just use a switch statement? Changing myFunction(desiredArray) depending on the case?
Marking this correct. I still haven't got it to work, but I'm sure you're right and I just haven't adapted it correctly yet. Might try a switch first.
0

Well, since there didn't seem to be an easy way, I decided to go with a sure-fire, clear way!

This switch is as basic as it gets. It takes someName, matches it to a case, and they all run the same function but an array specific to that someName value.

func passArrayToFunc () {
    switch someName {
    case "somename1":
        funcThatNeedsArray(someArray1)
    case "someName2":
        funcThatNeedsArray(someArray2)
    case "someName3":
        funcThatNeedsArray(someArray3)
    case "someName4":
        funcThatNeedsArray(someArray4)
    case "someName5":
        funcThatNeedsArray(someArray5)
    case "someName6":
        funcThatNeedsArray(someArray6)
    case "someName7":
        funcThatNeedsArray(someArray7)
    case "someName8":
        funcThatNeedsArray(someArray8)
    default:
        print("No matches")
    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.