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 trialBenjamin Pena
2,835 PointsCould 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
agreatdaytocode
24,757 PointsHi,
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)
}
}
Matt Barth
5,060 PointsYou might have forgotten to link your gesture recognizers to the showplaylistdetail IBaction segue function. Make sure they are all linked.
Ryan Nelson
2,369 PointsI'm running Xcode version 6.4 and getting the same error. Is there a solution?
Jordan Buonforte
6,013 Pointswas there ever a solution to this question? i am having the same problem
missingn
3,479 PointsI am also getting this error with Xcode 7.1.1 and Swift 2.0. A solution or workaround would be appreciated.
Mathieu Gosselin
3,288 PointsHad the same issue. In the main storyboard, the tap gesture Recognizer Outlet collection was tied to the parent view of the UIImageView.
- Right + control click on view item in storyboard
- click the X to remove the outlet collection link
- control + click drag from Playlist Image View 0 to Tap Gesture Recognizer
- That's it!
Benjamin Pena
2,835 PointsBenjamin Pena
2,835 PointsWhen I try that I get a error saying that "UIView is not convertible to UIImageView must use as! to force downcast"
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsYou also have "(index: 0)" I have
playlistDetailController.playlist = Playlist(index: index)
Benjamin Pena
2,835 PointsBenjamin Pena
2,835 PointsI switched it back to (index:index) but I'm still getting the error saying I must use as!
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsWhat xCode Version are you running?
Benjamin Pena
2,835 PointsBenjamin Pena
2,835 Points6.3
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsI'm running Version 6.2 (6C131e) maybe that's the reason?
Benjamin Pena
2,835 PointsBenjamin Pena
2,835 PointsProbably. Is there another way I could cast my sender image as a UIImageView?