0

我正在尝试从 swift 字典中获取一个值以显示为单元格文本。我收到错误:

类型“任何”没有下标成员

cell.audioLabel.text = audiofiles["filetitle"] <-line producing error

我相信我可能错误地设置了变量,使用 didSelectRowAt 从另一个表视图中使用 segue 传递值。

var audios = Array<Any>() <- gets from another controller

这是我当前的视图控制器代码:

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
    }

当我使用

print(audios)

我得到的结果是:

[demoApp.Audiofile(id: 1, filetitle: "样本文件一", filename: "breath5mins", fileformat: "mp3"), demoApp.Audiofile(id: 2, filetitle: "样本文件二", filename: "breath10mins ", fileformat: "mp3"), demoApp.Audiofile(id: 3, filetitle: "示例文件三", filename: "breath20mins", fileformat: "mp3")]

如何使用文件标题作为单元格的标签文本?

目标是显示标题并在单击单元格时打开另一个视图,并允许用户单击新视图上的按钮并播放 mp3 文件。

4

3 回答 3

2

Apparently the object is not a Swift dictionary, it's a custom class or struct.

You are fighting Swift's strong type system. Don't do that.

Declare audios with the static type

var audios = Array<AudioFile>()

and use dot notation instead of insecure KVC (which does not work anyway in this case).

cell.audioLabel.text = audiofiles.filetitle
于 2018-09-10T20:35:58.703 回答
1

您已将数组声明为Array<Any>- 这意味着该数组可以包含任何内容;数组的元素甚至不需要都是相同的类型。结果,Swift 编译器不知道它来自什么类型的东西audios[indexPath.row];它可以是字典,可以是数组,也可以是整数。当您尝试使用下标时,编译器会给您一个错误,因为它不知道该项目是否支持下标- 即 anInt不支持,并且Any可能是Int.

如果您知道实际类型,请不要在 Swift 中使用Anyor ;AnyObjectSwift 的类型安全允许它在编译时通过了解事物的类型来消除大量潜在的运行时问题。

print语句看来,您的数组包含Audiofile实例(可能是您定义的结构或类,而不是字典)。因此,您应该audios正确声明:

var audios = Array<Audiofile>()

然后,您可以访问对象的属性cellForRow

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
}
于 2018-09-10T20:37:28.643 回答
-1

您收到错误的原因Any是没有定义的对象。您可以将其转换为AudioFile这样的:

let audiofiles = audios[indexPath.row] as! AudioFile

If audiosis a array of AudioFileonly then 声明它,您可以按照您描述的方式使用它。

于 2018-09-10T20:30:56.993 回答