我是Swift和IOS的新手,将字典数据传递给其他uiview时遇到了一些问题,有人可以帮助我修复它吗?

LessonsTableViewController:

var mylessons = [
    ["title":"Posture", "subtitle":"Set up your body", "bgimage":"1", "lesimage":"l1"],
    ["title":"Breathing", "subtitle":"Breathing deeply", "bgimage":"2", "lesimage":"l2"],
    ["title":"Breathing", "subtitle":"Breathing Exercise", "bgimage":"3", "lesimage":"l3"],
    ["title":"Health", "subtitle":"Do’s & Don’ts", "bgimage":"4", "lesimage":"l4"]
]


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! LessonsTableViewCell

    let lessonsObject = mylessons[indexPath.row]

    cell.backgroundImageView.image = UIImage(named: lessonsObject["bgimage"]!)
    cell.titleLabel.text = lessonsObject["title"]
    cell.subtitleLabal.text = lessonsObject["subtitle"]

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "LessonSegue", sender: mylessons[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    let lessegue = segue.destination as! LessonDetailsViewController
    lessegue.SelectedLessons = mylessons
}

LessonDetailsViewController:
    @IBOutlet weak var LTitle: UILabel!

var SelectedLessons = [Dictionary<String, String>()]

override func viewDidLoad() {
    super.viewDidLoad()
    LTitle.text = SelectedLessons["title"]
    // Do any additional setup after loading the view.
}

最后,它有一个错误“无法为索引类型为'String'的'[Dictionary]'类型的值下标”。

最佳答案

首先,您的 SelectedLessons 类型错误。您需要使用类似

var SelectedLessons:Dictionary<String, String>?
的东西
您需要过去的正确对象。
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    let lessegue = segue.destination as! LessonDetailsViewController
    lessegue.SelectedLessons = sender as? Dictionary<String,String>
}

10-08 07:09