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

时间:2018-08-28 17:08:24

标签: ios swift uitableview nsstring segue

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

  

线程1:信号SIGABRT ,输出显示无法将类型'JobTableViewController'(0x10a20cb80)的值强制转换为'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,
    ]
}
}

1 个答案:

答案 0 :(得分:1)

您的问题是,您在performSegue中将self(类型为JobTableViewController)作为sender参数发送

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

然后将其投射到

sender as! String

将其转换为

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