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 Using a Navigation Controller With Segues Modifying the UI

Kenton Raiford
Kenton Raiford
5,101 Points

as! Down casting not working. Please help.

Trying to get past this video but I've been stuck for a week now. I can't continue the project without fixing this error. Xcode keeps telling me to put as! but it's not fixing the error. Has anyone figured it out ? :D

Brendan O'Brien
Brendan O'Brien
5,521 Points

I'm having a similar problem even after applying the Module fix suggested below. In my case, altering the down casting code from as to as! does fix the error, but for whatever reason, the string change specified that is passed via the segue doesn't occur. The default button text in the storyboard is overwritten by the PlaylistDetailViewController and therefore the button comes up blank. It would seem the "Yay etc." text is either not being passed. or is overwritten.

Here's my code:

PlaylistMasterViewController

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

    //Useful to separately code ACTIONS (aka METHODS) here

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showPlaylistDetail" {
             let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
        playlistDetailController.segueLabelText = "Yay! You pressed the button!"


        }
    }

PlaylistDetailViewController

import UIKit

class PlaylistDetailViewController: UIViewController {


    @IBOutlet weak var buttonPressLabel: UILabel!
        var segueLabelText: String = "Testing"


    override func viewDidLoad() {
        super.viewDidLoad()

        buttonPressLabel.text = segueLabelText


    }

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



}

6 Answers

Beatriz Macedo
Beatriz Macedo
9,903 Points

I was getting the "Consecutive Declarations On A Line Must Be Separated By ';'" (don't know if this was your error...) and I managed to fix it by retyping the line and adding as! as shown on the documentation (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html) for forced downcasting.

Can anyone please help, I have the same problem...

I am also facing the same problem. Please let me know if u fix it.

Will do Deepa Laxmi. And you to, please let me know if you find an answer :)

Mohammed Mohammed
Mohammed Mohammed
2,278 Points

Yes, the down casting is not working, it shows an suggest to unwrap the optional. So any help would be appreciated.

Yupin Hu
Yupin Hu
3,702 Points

for the "as!" thing i think it's an new swift thing, so yes you do need to use it in order to make it work.

Here is how i fixed it and my code First, use "as!" Then, go to the storyboard and click on the "PlaylistDetailViewController", click on "identity inspector", finally click the dropdown of "module" and select "Algorhythm".

Good luck!

james bunn
james bunn
2,377 Points

Any clue why the module must be selected as "Algorithm"?

Guillaume batier
Guillaume batier
1,387 Points

i found it and it's work for me

if segue.identifier == "showPlaylistDetail" { if let playlistView = segue.destinationViewController as? PlaylistDetailController{ playlistView.segueLabelText = "test loaded"

        }
  }
Teja Kanchinadam
Teja Kanchinadam
2,429 Points

This worked for me! Thank you! If anyone else is facing the same problem, please follow the code above and don't forget to remove the code "aButton.setTitle("Press me!", ForState: .Normal)" from the viewDid load method of the first view controller.