title: Swift: Pass data to static table view
body: <p>I'm starting to study Swift with the help of this Apple's guide: <a href="https://itunes.apple.com/it/book/app-development-with-swift/id1219117996" rel="nofollow noreferrer">https://itunes.apple.com/it/book/app-development-with-swift/id1219117996</a></p>
  
  <p>Now i am stuck on a small problem that i would like to understand how to solve, but from the guide it is not very clear.</p>
  
  <p>This is the situation</p>
  
  <p><a href="https://i.stack.imgur.com/9QT3K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9QT3K.png" alt="enter image description here"></a></p>
  
  <p>It's a simple application, that show a list of emojis with a description. A tap on emoji-cell it should open the static table view and show the emoji's information.</p>
  
  <p><strong>Result</strong></p>
  
  <p><img src="https://i.stack.imgur.com/tTcut.png" width="200">
  <img src="https://i.stack.imgur.com/S5PgZ.png" width="200"></p>
  
  <p>From the guide</p>
  
  <blockquote>
    <p>“You'll need to pass the Emoji object from EmojiTableViewController to
    the static table view controller when a cell is tapped. Start by
    creating a new file, and define a UITableViewController subclass
    called "AddEditEmojiTableViewController". Remember to delete or
    comment out any table view data source methods in the file. In
    Main.storyboard, set the static table view's "Custom Class" to
    "AddEditEmojiTableViewController". Add a property to the
    AddEditEmojiTableViewController class called emoji with a type of
    Emoji. Within the prepare(for segue:) method of
    EmojiTableViewController, you'll check the identifier, downcast the
    destination view controller, access the object(s) to pass, then assign
    the properties:”</p>
  </blockquote>
  
  <p><strong>AddEditEmojiTableViewController</strong> file</p>
  
  <pre><code>import UIKit
  
  class AddEditEmojiTableTableViewController: UITableViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          if let emoji = emoji{
              symbolTextfield.text = emoji.symbol
              nameTextfield.text = emoji.name
              descriptionTextfield.text = emoji.description
              usageDescription.text = emoji.usage
          }
  
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      var emoji: Emoji! = nil
  
      @IBOutlet weak var symbolTextfield: UITextField!
      @IBOutlet weak var nameTextfield: UITextField!
      @IBOutlet weak var descriptionTextfield: UITextField!
      @IBOutlet weak var usageDescription: UITextField!
  
  }
  </code></pre>
  
  <p>The <strong>prepare</strong> function of the <strong>EmojiTableViewController</strong> file, as described in the guide. <em>(EditEmoji is the segue from the cell to the static table view)</em></p>
  
  <pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "EditEmoji" {
          let indexPath = tableView.indexPathForSelectedRow!
          let emoji = emojis[indexPath.row]
          let addEditEmojiTableViewController = segue.destination
          as! AddEditEmojiTableViewController
          addEditEmojiTableViewController.emoji = emoji
      }
  }
  </code></pre>
  
  <p>Kepping the prepare function in this way, when i tap on a cell i have this error-crash</p>
  
  <pre><code>Could not cast value of type 'UINavigationController' .. to 'Emoji.AddEditEmojiTableTableViewController' ....
  ... Could not cast value of type 'UINavigationController' ... to 'Emoji.AddEditEmojiTableTableViewController' ...
  </code></pre>
  
  <p>So, looking on stackoverflow, i modified the function in this way, and now it works.</p>
  
  <pre><code> override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "EditEmoji"{
          let indexPath = tableView.indexPathForSelectedRow!
          let emoji = emojis[indexPath.row]
          let test1 = segue.destination as! UINavigationController
          let test2 = test1.topViewController as! AddEditEmojiTableTableViewController
          test2.emoji = emoji
      }
   }
  </code></pre>
  
  <hr>
  
  <p>So my question is: Is the guide wrong? Is the guide not updated with the latest Swift version? </p>