无法将类型 'String?' 的值转换为预期的参数类型 'Class?'

Cannot convert value of type 'String?' to expected argument type 'Class?'

我正在尝试使用库 Alamofire 获取数据,架构是 MVVM。有一个 class(Web 服务 class)调用 Alamofire 请求,但是当我解码时它显示错误“无法将类型 'String?' 的值转换为预期的参数类型 'Gregorian?'”。如何解决这个问题。

Prayer.swift

class Prayer: Codable {
    let code: Int?
    let status: String?
    let data: DataClass?

    init(code: Int?, status: String?, data: DataClass?) {
        self.code = code
        self.status = status
        self.data = data
    }
}

// MARK: - DataClass
class DataClass: Codable {
    let timings: Timings?
    let date: DateClass?
    let meta: Meta?

    init(timings: Timings?, date: DateClass?, meta: Meta?) {
        self.timings = timings
        self.date = date
        self.meta = meta
    }
}

// MARK: - DateClass
class DateClass: Codable {
    let readable, timestamp: String?
    let hijri: Hijri?
    let gregorian: Gregorian?

    init(readable: String?, timestamp: String?, hijri: Hijri?, gregorian: Gregorian?) {
        self.readable = readable
        self.timestamp = timestamp
        self.hijri = hijri
        self.gregorian = gregorian
    }
}

// MARK: - Gregorian
class Gregorian: Codable {
    let date, format, day: String?
    let weekday: GregorianWeekday?
    let month: GregorianMonth?
    let year: String?
    let designation: Designation?

    init(date: String?, format: String?, day: String?, weekday: GregorianWeekday?, month: GregorianMonth?, year: String?, designation: Designation?) {
        self.date = date
        self.format = format
        self.day = day
        self.weekday = weekday
        self.month = month
        self.year = year
        self.designation = designation
    }
}

WebService.swift

class WebService : NSObject{
    static let shareInstance = WebService()
    func getPrayerTimingsDataCurrentLocation(completion: @escaping(Timings?,Gregorian?, Error?) -> ()){
        let url = "http://api.aladhan.com/v1/timings/20%20June%202021?latitude=51.508515&longitude=-0.1254872&method=2"
        
        AF.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil).response {
            (responseData) in
            guard let data = responseData.data else {return}
            do {
                let prayerDate = try JSONDecoder().decode(Prayer.self, from: data).data?.date?.gregorian?.date
                let prayerTimings = try JSONDecoder().decode(Prayer.self, from: data).data?.timings
                completion(prayerTimings,prayerDate, nil) //Here getting error on 'prayerDate' Cannot convert value of type 'String?' to expected argument type 'Gregorian?'

            } catch {
                print(String(describing: error))
            }
        }
    }
}

您有以下行:

let prayerDate = try JSONDecoder().decode(Prayer.self, from: data).data?.date?.gregorian?.date

这会得到一个 String?,因为您正在访问 Gregoriandate 属性,它被定义为 String?

因为您只需要 Gregorian?,请删除 .date:

let prayerDate = try JSONDecoder().decode(Prayer.self, from: data).data?.date?.gregorian