0

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!"
Avi Zolty
  • 52
  • 5

1 Answers1

0

Because the segue.destinationViewController can be any kind of subclassed UIViewController, it needs to know the class it's based on so it knows what it's loading. It is creating an instance of the class in the variable that you define.

And you can use an if let if you'd like instead. See https://stackoverflow.com/a/29360924/4096655

Community
  • 1
  • 1
Fred Faust
  • 6,696
  • 4
  • 32
  • 55
  • So could you add another view controller with the same class and receive the same results? as it'll pull the right class and create an instance of segue.destinationViewController and save it in my constant? – Avi Zolty Jul 24 '15 at 03:24
  • Well I think the last assignment of the segue.destinationViewController will be what you get, if you add another one you'll just re-assign it. – Fred Faust Jul 24 '15 at 03:28
  • I mean could I substitute so `if let playlistDetailController = segue.destinationViewController as? CompletelyDiffViewControllerWithSameClassJustToPullTheClassFrom` Or is it doing anything more than just pulling the class – Avi Zolty Jul 24 '15 at 03:31
  • Oh I understand now, no you can't, you need to create an instance of the class that the segue is connected to in the storyboard. – Fred Faust Jul 24 '15 at 03:39