[英]Appending Model into array in Swift
因此,我創建了一個模型類,在其中使用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() {
...
}
}
在這里,您可以強制展開NotificationModel。 您正在嘗試訪問nil值的類型。 請嘗試以下修復。
var notificationModel: NotificationModel! var notification = [NotificationModel]()
if notificationModel?.type == "meeting" {
notification.append(notificationModel!)
}
希望這能解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.