I am trying to get a value from a swift dictionary to display as cell texts. I am getting error:
Type 'Any' has no subscript members
cell.audioLabel.text = audiofiles["filetitle"] <-line producing error
I believe I may have set the variables incorrectly, the value is being passed using segue from another tableview using the didSelectRowAt.
var audios = Array<Any>() <- gets from another controller
This is my current viewcontroller code:
import UIKit
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var audioTable: UITableView!
@IBOutlet weak var descText: UITextView!
@IBOutlet weak var clickButton: UIButton!
var practitle = String()
var desc = String()
var hasAudio = Int()
var audios = Array<Any>()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = practitle
descText.text = desc
if hasAudio != 0 {
clickButton.isHidden = false
}
print(audios)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return audios.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "audiocell", for: indexPath) as! DetailViewCell
let audiofiles = audios[indexPath.row]
cell.audioLabel.text = audiofiles["filetitle"]
return cell
}
When I use
print(audios)
The result that I get is:
[demoApp.Audiofile(id: 1, filetitle: "Sample file one", filename: "breath5mins", fileformat: "mp3"), demoApp.Audiofile(id: 2, filetitle: "Sample file two", filename: "breath10mins", fileformat: "mp3"), demoApp.Audiofile(id: 3, filetitle: "Sample file three", filename: "breath20mins", fileformat: "mp3")]
How can I use the filetitle as the label texts for my cell?
The goal is to display the title and open another view on cell click and allow the user to click a button on the new view and play the mp3 file.