簡體   English   中英

致命錯誤NSArray元素在xcode10.1更新后無法與Swift數組元素類型匹配

[英]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.

在調試之后貶低更多的specefic

錯誤消息告訴您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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM