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

Richard Price
Richard Price
2,050 Points

crashing on adding sender!.view as UIImageview

does anyone know the fix for this? crashes after adding the following code

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

                 if segue.identifier == "showPlaylistDetailSegue" {

               let playlistImageView = sender!.view as UIImageView

                if let index = find(playlistArray, playlistImageView) {



        let playListDetailController = segue.destinationViewController        

        as   

          PlaylistDetailViewController

               playListDetailController.playlist = Playlist(index: index)

            }

        }

    }

9 Answers

Ryan Haider
PLUS
Ryan Haider
Courses Plus Student 2,418 Points

As others have said, you have to remove the tap gesture and connected action and then re-add.

To do that: Go to Main.storyboard. Click on the view structure, playlists scene, Tap Gesture Recognizer. In the Connections Inspector click the 'X' on the sent actions. Now click on the Tap Gesture and hit delete. Add a new tap gesture to the placeholder image. In the connections pane, drag from sent actions showPlaylistDetail over to the placeholder image. Save and run.

Richard Price
Richard Price
2,050 Points

this is basically the code in the previous video. Incorrect as it doesn't solve the casting issue. I can easily return to the previous code but thats not what I'm looking to do

Okay, I am "sorry". I trie to find the answer for your problem!

I think the problem is the "index". But I am not sure!

Okay.

Steven Hancock
Steven Hancock
3,394 Points

I've got the same problem. Crashing on that line. Someone please help this is very frustrating.

For me the solution was Pasans answer to the "Swift Dynamic Cast Failed" question. The Tap Gesture Recognizer was connected to the main View and not the ImageView. I don't know how to link there bit you will find it in the Questions part below the "Passing Playlist Objects Using Segues" video.

I had this problem too. I just reset the tap gesture and the IBAction and it worked.

pls help, how do you reset the tab gesture and the IBAction?

I had the same issue - the code is correct but I think the way Pasan connected the Tap Gesture Recognizer in the storyboard was incorrect for this step to work.

I deleted it, and the segue, and dropped the tap gesture again - this time on the ImageView and command dragged from the ImageView to the Detail scene in the storyboard and it worked.

Hello, i had the same issue. resolved it by doing the following: deleting the segue and the tap gesture recognizer then add them again (may have to watch the video 'adding touch capabilities' again)

Hello Richard Price. I am "sorry" if I misunderstood you. Here is my solution for you.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showPlaylistDetail" {
        let playlistDetailController = segue.destinationViewController as DetailViewController
        playlistDetailController.playlist = Playlist(index: 0)

    }
}

I hope it helps you! : )

john