簡體   English   中英

Swift 4:無法將類型“ [X]”的值分配給類型“ [VC.X]”

[英]Swift 4: Cannot assign value of type '[X]' to type '[VC.X]'

如何消除此編譯器錯誤? 如果我在函數中聲明項目,則所有工作正常。

class VC: UIViewController {
    struct X: Decodable  {
        let id: String
        let name: String
        let url: String
    }
    var items = [X]()

    //-----------------------------

    func somefunc()  {
        do {
            // the error is here:
            self.items = try JSONDecoder().decode([X].self, from: data)    <<<<<<<<<<< Compile error is here
        }
        catch  let err  {
        }
    }
}         // class VC

應該可以工作(我嘗試過,沒有編譯器錯誤),但是仍然,也許您已經有其他具有相同名稱的類/結構。 請嘗試使用此方法來確保:

self.items = try JSONDecoder().decode([VC.X].self, from: data) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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