0

I have multiple arrays, The first one contains the names of all the other arrays.

So I retrieve the value of the first array I get the name of my desired array but I don't know how to proceed from there. As swift sees my result as a string, not a variable name. I have recreated my problem below

import UIKit
var str = "Hello, playground"
var traits = ["trait one","trait 2", "trait3"]
var trait3 = ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"]
var counter = 2
let intermediateV = traits[counter]
print(intermediateV[2])

The error I get is:

'subscript' is unavailable: cannot subscript String with an Int

4
  • 1
    traits[counter] is "trait3", what is intermediateV[2] aka "trait3"[2] supposed to do? Are you trying to get "final Answer 2" - that is not going to work. Use multi-dimensional arrays instead or (even better) structs and classes, do not create variables with counters as part of their name, that is almost always a bad idea. Commented Aug 11, 2017 at 11:07
  • I am indeed trying to get Final Answer 2. I understand this is not going to work, do you have any suggestions on how to get my desired result? Commented Aug 11, 2017 at 11:09
  • I cant use multi-dimensional arrays as the first array aka "traits" is downloaded from Firebase. The other array aka "trait3" is stored locally. I need a way to get "Final answer 2" where these two arrays are seperate. Commented Aug 11, 2017 at 11:13
  • You are trying to replace a variable name with a literal string. This is impossible. Variable names are evaluated at compile time. Put the arrays (the objects) rather than the literal variable names into the array. Commented Aug 11, 2017 at 11:15

2 Answers 2

1

If you cannot do multi dimensional arrays you have to use dictionaries:

var traitKeys = ["trait one", "trait 2", "trait3"] // downloaded from somewhere, dynamic

var traits = [
    "trait one" : ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"], 
    "trait 2" : ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"], 
    "trait3" : ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"]
]

let counter = 2
let traitKey = traitKeys[counter]

print(traits[traitKey] ?? "trait not found")
Sign up to request clarification or add additional context in comments.

Comments

1

You are trying to access an element from String intermediateV equals "trait3" in your case. However string subscription is not possible in your case. You can try something like this:

var trait1 = ["trait 0","trait 1","trait 2","trait 3","trait 4"]
var trait2 = ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"]
var trait3 = ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"]

var traits = [trait1, trait2, trait3]

var counter = 2

let intermediateV = traits[counter]

print(intermediateV[2])

1 Comment

As I mentioned in the comments, I cant do multi-dimensional arrays as the first array "traits" is downloaded from the internet so it has values of strings inside it. I need to use those strings to lookup my other locally stored arrays

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.