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 Tapping On Our Playlists

Build failed question in Xcode 7

I am trying to preview my app but I am having trouble with the code in the Master View Controller. Here is my code:

import UIKit

class PlaylistMasterViewController: UIViewController {

var playlistsArray: [UIImageView] = []
@IBOutlet weak var playlistImageView0: UIImageView!
@IBOutlet weak var playlistImageView1: UIImageView!
@IBOutlet weak var playlistImageView2: UIImageView!
@IBOutlet weak var playlistImageView3: UIImageView!
@IBOutlet weak var playlistImageView4: UIImageView!
@IBOutlet weak var playlistImageView5: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()

    playlistsArray += [playlistImageView0, playlistImageView1, playlistImageView2, playlistImageView3, playlistImageView4, playlistImageView5]

    for index in 0..<playlistsArray.count {
        let playlist = Playlist(index: index)
        let playlistImageView = playlistsArray[index]

        playlistImageView.image = playlist.icon
        playlistImageView.backgroundColor = playlist.backgroundColor
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showPlaylistDetailSegue" {

        let playlistImageView = sender!.view as! UIImageView
        if let index = indexOfAccessibilityElement (playlistsArray, playlistImageView)
        {
            let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
            playlistDetailController.playlist = Playlist(index: index)
        }


    }
}

@IBAction func showPlaylistDetail(sender: AnyObject) {
    performSegueWithIdentifier("showPlaylistDetailSegue", sender: sender)
}

}

I am having an issue with this part

if let index = indexOfAccessibilityElement (playlistsArray, playlistImageView)

The instructor used find, but it did not work in Xcode 7 and prompted me to use indexof, but i still can't get it to work and my build will not complete.

Thank you!

2 Answers

Hi Victoria,

Try this:

            if let index = playlistsArray.indexOf(playlistImageView){
                let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
                playlistDetailController.playlist = Playlist(index: index)
            }

Everything else should stay the same :)

That worked! Thank you so much!

Ravirayappan Chinnappan
PLUS
Ravirayappan Chinnappan
Courses Plus Student 11,293 Points

How can i rectify this... i got an error on find key word how to use???? Plz help me

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "showPlaylistDetailSegue"
{

    let playlistImageView = sender!.view as! UIImageView

    if let index = find(playlistsArray, playlistImageView) {
        let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
        playlistDetailController.playlist = Playlist(index: index)
    }


}

}