1

Is it possible to access a string inside an array that is inside another array? - Swift

for instance:

    var a = 1
    var b = 2

    var maleDogs = ["Fido","Thor"]
    var femaleDogs = ["Linn","Eva"]
    var dogs = [maleDogs,femaleDogs]

And then do something like

dogs[a][b]

In this instance, I wanted to get "Thor" as an output, but it calls an error. (Inside playground)

2
  • Have you tried that? Commented Jan 12, 2015 at 14:07
  • 4
    @b3rge: Your code is completely correct, but note that Swift array indices are zero-based, so accessing index 2 will cause an out-of-bounds exception. Commented Jan 12, 2015 at 14:11

1 Answer 1

3

Your code is completely correct, but note that Swift array indices are zero-based, so accessing index 2 causes an "Array index out of range" exception. You can see the error message if you open the "Assistant Editor" for the Playground file (View -> Assistant Editor -> Show Assistant Editor).

What you probably wanted is

var a = 0
var b = 1

var maleDogs = ["Fido","Thor"]
var femaleDogs = ["Linn","Eva"]
var dogs = [maleDogs,femaleDogs]

dogs[a][b]  // Thor
Sign up to request clarification or add additional context in comments.

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.