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!

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

Benjamin Pena
Benjamin Pena
2,835 Points

Could not cast value of type UIView to UIImageView

When building a playlist App for iOS, I get the above error in this code.

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: 0)
        }

    }

6 Answers

Hi,

You have "as!" instead of "as"

// let playlistImageView = sender!.view as UIImageView

Try this:

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)
        }

    }
Benjamin Pena
Benjamin Pena
2,835 Points

When I try that I get a error saying that "UIView is not convertible to UIImageView must use as! to force downcast"

You also have "(index: 0)" I have

playlistDetailController.playlist = Playlist(index: index)

Benjamin Pena
Benjamin Pena
2,835 Points

I switched it back to (index:index) but I'm still getting the error saying I must use as!

What xCode Version are you running?

I'm running Version 6.2 (6C131e) maybe that's the reason?

Benjamin Pena
Benjamin Pena
2,835 Points

Probably. Is there another way I could cast my sender image as a UIImageView?

Matt Barth
Matt Barth
5,060 Points

You might have forgotten to link your gesture recognizers to the showplaylistdetail IBaction segue function. Make sure they are all linked.

I'm running Xcode version 6.4 and getting the same error. Is there a solution?

Jordan Buonforte
Jordan Buonforte
6,013 Points

was there ever a solution to this question? i am having the same problem

I am also getting this error with Xcode 7.1.1 and Swift 2.0. A solution or workaround would be appreciated.

Mathieu Gosselin
Mathieu Gosselin
3,288 Points

Had the same issue. In the main storyboard, the tap gesture Recognizer Outlet collection was tied to the parent view of the UIImageView.

  1. Right + control click on view item in storyboard
  2. click the X to remove the outlet collection link
  3. control + click drag from Playlist Image View 0 to Tap Gesture Recognizer
  4. That's it!