Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Playlist Browser with Swift Refactoring Our Code Passing Playlist Objects Using Segues

How does the find works in the case?

How does the find works in this case

1 Answer

Martin Shay
Martin Shay
5,919 Points

the "find" function is taking two parameters - a "domain" and a "value". (in this case, "domain" == "playlistsArray", and "value" == "playlistImageView".)

if the "value" exists in the "domain", the "find" function returns the index of "value". if it does not, it returns "nil".

S B
S B
4,497 Points

I have a follow-up question abut the above response. In the background, does the 'find' function simply match the 'image' and 'background' properties with the exact 'image' and 'background' properties of the respective index item set in the array by the 'for' loop in viewDidLoad? I'm trying to understand the logic behind the code. Any insight would be greatly appreciated. Thank you.

Noel Deles
Noel Deles
8,215 Points

Hi S B

Option Clicking on the function returns this definition: "Returns the first index where value appears in domain or nil if value is not found."

So like Martin said, it s one and only function is to return the index if there is a match and that is it. It goes looking for the value, and if the value is in that array, then it gets the index of that value in the array. How I understand it is that matching the image and background properties would serve no purpose in the find function's function.

S B
S B
4,497 Points

Thanks Noel. It is making more sense now ...however, if the "value" is "playlistImageView", once the user taps on the specific playlist on the main view, does the find function check to see which item in the array (if any) has the same .image and .backgroundColor values, and if there is a match, returns the index?