[英]fatal error: NSArray element failed to match the Swift Array Element type
[英]Fatal error NSArray element failed to match the Swift Array Element type after xcode10.1 update
因為我更新了xcode 10.1,就會發生這個錯誤。
代碼是:
class mainPageJobObj {
var _id : String = ""
var avatar :String = ""
var name : String = ""
var serviceState = ""
}
items : [mainPageJobObj]!
items // i read it from server and its not empty
let oneItem = items[index] // Fatal error: NSArray element failed to match the Swift Array Element type -- This is what compiler is showing.
錯誤消息告訴您items
是從NSArray
橋接的(它不強制鍵入其元素)但是當Swift嘗試檢索元素時,它沒有找到mainPageJobObj
的實例,這就是您所說的數組所包含的內容。
從調試屏幕截圖中可以看到items
數組實際上包含NSMutableDictionary
實例。
快速解決方法是使用適當的items
聲明:
var items:[String:Any] = slider.mainPageObj.items!
let oneItem = items[index]
let name = oneItem["name"]
一個更好的解決方案是從一開始就使用Swift類型; 如果以JSON格式返回服務器中的數據,則使用Codable
創建Swift Struct或Class實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.