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

Paul Denlinger
Paul Denlinger
2,380 Points

Program crashes on tapping image, returns SIGABRT error "Could not cast value of type 'UIView' to 'UIImageView'

Am running Xcode 6.3 and Swift 1.2, and this line causes problems for me. The code compiles, but crashes when I tap the image:

let playlistImageView = sender!.view as! UIImageView

in the following method:

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)

        }
    }
}

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


}

4 Answers

Michael Gamboa
Michael Gamboa
5,898 Points

After an hour of googling and re-watching videos, I disconnected the UITapGesture, disconnected and deleted the button. I believe my problem was when I added the UITapGesture I added it to the UIView and not the "placeholder" image or "UIIMageView". By re-adding it to the placeholder image, everything worked.

This google search within the treehouse Forum really helped get me back on track... https://teamtreehouse.com/forum/crashing-on-adding-senderview-as-uiimageview

I am still curious why in my version of XCode I have to use "as!" to downcast as described by Paul in above code...

Corey Dutson
Corey Dutson
Courses Plus Student 3,193 Points

Thanks for this! I ended up doing the same thing and scratching my head for a while. ended up having to remove the tap gesture and re-adding it (making sure it was bonded to the imageview and not the view)

Paul Denlinger
Paul Denlinger
2,380 Points

"I am still curious why in my version of XCode I have to use "as!" to downcast as described by Paul in above code..."

as! is a change introduced in Swift 2.0. The exclamation mark is a kind of forced unwrapping.

Steven Roseman
Steven Roseman
2,975 Points

Thanks, this really helped out.

Tukang Keboen
Tukang Keboen
2,840 Points

go to main.storyboard >> Tap Gesture Recognizer >> then go to connection inspector, is the gestureRecognizers connect to Playlist Image View? View instead right. try put cursor on dot View and it will show + then drag to the placeholder, final delete the old View it should be work now

If the above fix still didn't work for you, make sure that the playlists place holder image has 'User Interaction Enabled' you'll find this checkbox by selecting the placeholder then attributes inspector. For some reason I had to go back an re-enable it.