繁体   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