将模型附加到Swift中的数组中

时间:2017-06-14 06:01:15

标签: ios model-view-controller swift3 alamofire

所以我创建了一个模型类,我正在使用Alamofire从API获取数据。我想在表中显示该数据以便查看,所以我想将该模型附加到VC中的数组中并在自定义单元格中调用它。

但是,只有当键值与特定字符串匹配时,我才想将该模型附加到数组中。

然而,当我在VC中使用简单的if语句时,它给我一个致命的错误,说错误的指令。

代码

调用模型

var notificationModel: NotificationModel!
var notification = [NotificationModel]()

viewDidLoad中

if notificationModel.type == "meeting" {
    self.notification.append(notificationModel)
}

模型

Class NotificationModel

    var _type: String!

    var type: String {
        if _type == nil {
            _type = "Err"
        }
        return _type
    }

    func downloadData() {
          ... 
    }  
}

1 个答案:

答案 0 :(得分:0)

这里你有强制解包NotificationModel。而您正在尝试访问零值的类型。 请尝试使用以下修复程序。

var notificationModel: NotificationModel! var notification = [NotificationModel]()

    if notificationModel?.type == "meeting" {
notification.append(notificationModel!)

}

希望这可以解决问题。