I'm pretty new to programming and I've been stuck on this for some time. I have a struct called Patient which is full of vars, eg var FIRST: String.
I've already written the code to retrieve data from JSON and populate this struct. It works fine, I can call print(Patient) and I see all the data in the console.
The instance of the struct is a 2d array so when I print to the console I get Patient(FIRST: "Bob", LAST: "Smith") Patient(FIRST: "Dave", LAST: "Evans") Patient(FIRST: "Liz", LAST: "Taylor")
The first tableview method works fine and returns a count of 3.
The second is where I'm stuck:
I declared:
static var globalPatientInstance: [Patient] = []
This is then populated with data from a completion handler. Here's the second method:
extension ViewController: NSTableViewDelegate {
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let item = ViewController.globalPatientInstance[row]
let cell = tableView.makeView(withIdentifier: (tableColumn!.identifier), owner: self) as? NSTableCellView
cell?.textField?.stringValue = item[(tableColumn?.identifier)!]!
return cell!
}
}
This gives "Type 'Patient' has no subscript members"
However if I change the line to be:
cell?.textField?.stringValue = item.FIRST
The code will run and I get three rows in the table view with each row having a different name - Bob, Dave, Liz.
What is the correct way to get the rest of the data, ie item.LAST to display also?
Thanks for any help, I've tried so many different things but I'm just really stuck on this.
"Type 'Patient' has no subscript members" - This error is because you are trying to access Patient as an array. Item is a struct, not an array.
When you say your struct is a 2D array, that doesn't make sense. Your struct is a struct. Your "2D" array is just a global instance variable array of structs. [Patient] is a 1D array.
let item = ViewController.globalPatientInstance[row] // item is now a Patient struct
let cell = tableView.makeView(withIdentifier: (tableColumn!.identifier), owner: self) as? NSTableCellView
cell?.textField?.stringValue = item[(tableColumn?.identifier)!]! // This doesn't work because Patient is not an array, but a struct
cell?.textField?.stringValue = item.FIRST // This works because you are accessing the property of item
cell?.textField?.stringValue = item.LAST // This is to display the LAST property of item
return cell!
}
Note -- Please name your class properties camelCase.. You can use this styleguide as a newbie reference..
https://github.com/raywenderlich/swift-style-guide
Also -- using first and last are bad naming conventions for a property because it sounds like first is the first element of an array. I would recommend firstName and lastName.
I figured it out. Thanks for your help guys. This method does what I need:
extension ViewController: NSTableViewDelegate {
fileprivate enum CellIdentifiers {
static let firstName = "FIRST"
static let lastName = "LAST"
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var cellValue: String = ""
var cellIdentifier: String = ""
let item = ViewController.globalPatientInstance[row]
if tableColumn == tableView.tableColumns[0] {
cellValue = item.FIRST
cellIdentifier = CellIdentifiers.firstName
}
else if tableColumn == tableView.tableColumns[1] {
cellValue = item.LAST
cellIdentifier = CellIdentifiers.lastName
}
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellIdentifier), owner: nil) as? NSTableCellView {
cell.textField?.stringValue = cellValue
//cell.textField?.stringValue = lastName
return cell
}
return nil
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.