29205

NSIndexPath does not have a member named row in swift

Question:

I have this code:

class ViewController: UITableViewController { var myArr = [(id:1, name:"1123", description:"d1", image:"palm-tree.png"), (id:2, name:"23", description:"d2", image:"palm-tree.png"), (id:3, name:"3", description:"d3", image:"palm-tree.png"), (id:4, name:"4", description:"d4", image:"palm-tree.png")] override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showDetail" { var indexPath = tableView.indexPathForSelectedRow() let destinationViewController:DetailViewController = segue.destinationViewController as DetailViewController destinationViewController.myArr = myArr[indexPath.row] } } }

And I have an error <strong>NSIndexPath does not have a member named row</strong>. How to fix that?

Thanks!

<strong>Update:</strong>

class ViewController: UITableViewController { struct Item { let id: Int let name: String let description: String let image: String } var myArr = [Item(id:1, name:"1123", description:"d1", image:"palm-tree.png"), Item(id:2, name:"23", description:"d2", image:"palm-tree.png"), Item(id:3, name:"3", description:"d3", image:"palm-tree.png"), Item(id:4, name:"4", description:"d4", image:"palm-tree.png")] override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showDetail" { if let indexPath = tableView.indexPathForSelectedRow() { let destinationViewController = segue.destinationViewController as DetailViewController destinationViewController.myArr = [myArr[indexPath.row]] } } } }

And <strong>DetailViewController</strong>:

class DetailViewController: UIViewController { struct Item { let id: Int let name: String let description: String let image: String } var myArr = [Item(id:1, name:"1123", description:"d1", image:"palm-tree.png")] }

Answer1:

tableView.indexPathForSelectedRow() returns an Optional NSIndexPath -- you need to unwrap it to access the row property:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showDetail" { if let indexPath = tableView.indexPathForSelectedRow() { let destinationViewController = segue.destinationViewController as DetailViewController destinationViewController.myArr = [myArr[indexPath.row]] } } }

Recommend

  • send data to DetailView from FavoriteTableView?
  • Dismissing Popover View with Storyboard Segues
  • Preparing for segue in embedded tableView in Swift
  • Xcode : Storyboard and retaining data in each controller
  • Delegation - Change variables from different viewsControllers
  • How do I access text in UITextField from another ViewController (Swift)?
  • How to find three largest and three smallest decmicals using Math.Max, Math.Min
  • App freezes when segue performed from button
  • Creating an instance of a class with the same type as an existing object
  • Will VB.NET automatically generate ComClass attribute and guids?
  • How to do a Lucene search with Sitecore item with specific date?
  • unrecognized selector sent to instance error when a button is clicked
  • Swift Cast Generics Type
  • Replace value in unordered list (html) + JQuery
  • Conflicting Types Error
  • d3 drag jumping to other position when clicked - origin?
  • Google App Indexing not resolving for Swift?
  • Tips for creating scalable WPF user control
  • Get attributes of existing SVG elements and bind as data with d3.js
  • Google Cloud Platform - Vanity Nameservers
  • How do I pass worksheet and ranges as variables?
  • Convert Func to Func
  • Creating interactive SVG maps for a web portal
  • iOS 9 errors and correct conversion to swift 2
  • Custom Nav Title offset ios 11
  • Click on button in another program - FindWindow, C#
  • How to access profile picture with Facebook API in Swift 3?
  • Floated image with variable width and heading with background image
  • c# open webrowser in many tab
  • How to have background script and something similar to a default popup?
  • How to Cache Real-time Data?
  • Functions in global context
  • Python urlparse: small issue
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • GridView Sorting works once only
  • AT Commands to Send SMS not working in Windows 8.1
  • unknown Exception android
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • failed to connect to specific WiFi in android programmatically