【问题标题】:How to cast to an array of objects?如何转换为对象数组?
【发布时间】:2020-02-12 07:39:41
【问题描述】:

在 CourseViewController 我有以下变量:

var courses =  [Course]()

在顶部声明

我尝试通过这样做来继续使用 CourseViewController

   let dc = segue.destination as! CourseListViewController
   dc.courses = items[tag] as! [Course]

这是正确的做法吗? “items[tag] as![Course]”这样对吗?

【问题讨论】:

  • "这对吗?"你试过运行你的代码吗?你的代码有效吗?如果是,那么您的代码是正确的。
  • 它不起作用,这就是我问的原因
  • 当然检查响应是否确实是一个数组。
  • @PotaOnasys 你解决了吗?
  • 什么是items

标签: swift casting


【解决方案1】:

如果您确定该标签上的项目肯定是[Course],那么它绝对正确。如果没有,请使用if-letguard 以避免运行时异常

 if let courses = items[tag] as? [Course]{
    dc.courses = courses
 } 

【讨论】:

    猜你喜欢
    • 2018-02-13
    • 2022-01-23
    • 2011-01-29
    • 2018-07-27
    • 2018-12-12
    • 1970-01-01
    相关资源
    最近更新 更多