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 Adding Multiple Playlists

Aman Kumar
Aman Kumar
10,444 Points

How does playlistImageView finds the index ?

How does the statement find the index that we can use in if let statement

  let playlistImageView = sender?.view as! UIImageView


  if let index = find(playlistsArray, playlistImageView) {
            println(index)
            playlistDetailController.playlist = PlaylistDictionary(index: index)
        }
Chris Stromberg
Chris Stromberg
Courses Plus Student 13,389 Points
  1. You touch a playlist Image.
  2. "sender?.view" recognizes what image you touched and returns a object.
  3. the object returned is than cast as a UIImageView type and assigned to a constant named "playlistImageView".

  4. if let index = find(playlistsArray, playlistImageView) is a built in function that uses two parameters sequence and element.

The sequence parameter looks at the playlistsArray and searches to see if your playlistImageView is within that sequence(array). If it does find that matching element, it returns the index number where that element resides.

This index number is now passed to our PlaylistDictionary(index: index) function and we assign the newly initialized object to the playlistDetailController property named playlist.

1 Answer

Tukang Keboen
Tukang Keboen
2,840 Points

xcode 7.0

let playlistImageView = sender!.view as! UIImageView

if let index = playlistsArray.indexOf(playlistImageView)