无法将 'TableViewController' 类型的值转换为 'NSString'

Could not cast value of type 'TableViewController' to 'NSString'

我在下面代码的第四行遇到以下错误...

Thread 1: signal SIGABRT, and the output says Could not cast value of type 'JobTableViewController' (0x10a20cb80) to 'NSString' (0x10cc5a2a8).

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toDetails" {
        let detailController = segue.destination as! JobDetailViewController
        detailController.valueToPass = sender as! String
    }
}

Job.swift:

class Job {
var text: String = ""
let ref: DatabaseReference!

init(text: String) {
    self.text = text
    ref = Database.database().reference().child("jobs").childByAutoId()
}

init(snapshot: DataSnapshot)
{
    ref = snapshot.ref
    if let value = snapshot.value as? [String : Any] {
        text = value["text"] as! String
    }
}

func save() {
    ref.setValue(toDictionary())
}

func toDictionary() -> [String : Any]
{
    return [
        "text" : text,
    ]
}
}

您的问题是,在 performSegue 中,您发送 self(属于 JobTableViewController 类型)作为 sender 参数,如下所示

self.performSegue(withIdentifier: "toDetails", sender: self)

然后将其转换为

sender as! String

所以将其转换为

self.performSegue(withIdentifier: "toDetails", sender:"sendedStr")