0

I have an array called [images]. Within this array, there is the ID of each image, and the URL of each image. I wish to put only the URL (not the ID) of the selected image into a variable to be used elsewhere.

For example, when I print my entire array:

print(images)

I get the following:

[(id: "nni", URL: "https://bbc.com/a1.jpg"), (id: "9hr", URL: "https://bbc.com/a2.jpg")]

I am able to successfully isolate the selected image using the following

print(images[indexPath.row)

So, if I click on the first image, this print statement will return:

[(id: "nni", URL: "https://bbc.com/a1.jpg")] // This is correct

However, I ONLY want to isolate that URL, and put it into a new variable, selectedURL

selectedURL = "https://bbc.com/a1.jpg"

How can I do this?

1

1 Answer 1

2

It looks like you have an array of tuples (not the best choice).

Do:

let selectedURL = images[indexPath.row].URL

or:

let selectedURL = images[indexPath.row].1

But consider creating a struct with id and URL fields.

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

2 Comments

Thanks - what makes the struct a better choice?
A struct is clear. It can have initializers and methods. It's easier to pass around.

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.