如何解决无法使用Swift将“ DataClass”类型的值分配为“ [DataClass]”类型的错误

时间:2019-08-29 19:24:25

标签: ios swift

我正在使用JSON实现Codable数据。在这里,我无法分配let数据:将DataClass值放入数组。我收到错误"Cannot assign value of type 'DataClass' to type '[DataClass]’。该如何解决?

我的JSON可编码

// MARK: - Welcome
struct Welcome: Codable {
    let status: Bool
    let data: DataClass
}

// MARK: - DataClass
struct DataClass: Codable {
    let agid, teamid, genname, dataDescription: String
    let schools: [School]
    let games, reminder: [String]

    enum CodingKeys: String, CodingKey {
        case agid, teamid, genname
        case dataDescription = "description"
        case schools, games, reminder
    }
}

// MARK: - School
struct School: Codable {
    let name, team: String
}

我的JSON加载代码

func loadJSON(){
    let urlPath = ""
    let url = NSURL(string: urlPath)
    let session = URLSession.shared
    let task = session.dataTask(with: url! as URL) { data, response, error in
        guard data != nil && error == nil else {
            print(error!.localizedDescription)
            return
        }
        do {
            let decoder = try JSONDecoder().decode(Welcome.self,  from: data!)
            let status = decoder.status
            if status == true {
                self.schoolsData = decoder.data.schools
                self.filteredSec = decoder.data // Getting Error Here
                DispatchQueue.main.async {
                    //self.tableView.reloadData()
                }
            } else {

            }
        } catch { print(error) }
    }
    task.resume()
}

0 个答案:

没有答案