I'm trying to understand how the following code works/ why it is required:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showPlaylistDetails"
let playlistDetailController = segue.destinationViewController as! PLaylistDetailViewController
playlistDetailController.segueLabelText = "Yay! It worked"
}
I understand the need to overide the default 'prepareForSegue' function -- and then specify that it is a UIStoryboardSegue. Furthermore, I understand the need to put an if statement that checks if the identifier I named for this particular segue is the right one (to make sure I'm talking about the right segue)
However, where I get lost is the
let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
I did a bit of research and understand that you need to downcast one type of element to it's 'real' subclass. The example the apple docs gave was an array including several objects of two separate subclasses- and needing to loop through them and treat each object as it's 'real' subclass- not as it's parent class just because that's what it had in common with everything else in the Array.
What I don't understand is why we need to have the segue.destinationViewController act as 'PlaylistDetailViewController' (The name of the controller my segue is leading to.)
What I assume but have yet to find anywhere that explains properly, is that segue.destinationViewController is (no pun intended! :P) PlaylistDetailViewController. But in this function I need to use the dot method of the segue (I'm assuming Class?) HOWEVER, segue doesn't have the ability to refer to variables inside the controller (maybe because it is too ambiguous? or as a safety precaution, because it may be used often in the code).
But then why does it require me to unwrap PlaylistDetailViewController -- if we know that they are the same then we know it won't return nil. And if it possible to return nil on a destination (which I don't think it is?) why do we not need to unwrap segue.destinationViewController ? Not to mention we're using a constant playlistDetailController- why wouldn't we just make it an if let to unwrap.
It seems with the simplicity of swift we should be able to say
segue.destinationViewController.segueLabelText = "Yay it worked!"
Or at least
PlaylistDetailViewController.segueLabelText = "Yay it worked!"