简体   繁体   中英

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

How can I eliminate this compiler error? If I declare items inside the function, all works.

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

Should be working (I tried it and there was no compiler error), but still, maybe you have already some other class/struct with the same name. Try using this instead just to be sure:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM