无法将“结果”类型的值转换为预期的参数类型“ EmailContentData”

时间:2019-11-07 13:00:07

标签: ios swift types casting type-conversion

enum Result {
case success(r: EmailContentData)
case failure(e: Error)}


static func getMessageList(index : Int, pageCount: Int, completion : @escaping (EmailContentData) -> ())
{
    DispatchQueue.global(qos: .userInteractive).async
        {
            let apiString : String = String(index) + "/" + String(pageCount)
            var objEmailContentData : EmailContentData? = EmailContentData.init(fromDictionary: NSDictionary())

            ApiHelper.sharedSession.post(Constant.API.messagelistApi + apiString, postData: NSDictionary(), methodtype: Constant.API.httpGet) { (isError, data, errorDescription) in
                guard let data = data else { return }
                do {
                    let result = EmailContentData.init(fromDictionary: data.value(forKey:"data") as! NSDictionary)
                    completion(Result.success(r: result))
                } catch let error {
                    completion(Result.failure(e: error))
                }
            }
    }
}

我成功遇到错误“无法将类型为“结果”的值转换为预期的参数类型为“ EmailContentData”” 且类似于完成错误“无法将类型为“结果”的值转换为预期的参数类型为“ EmailContentData””

请有人建议我我做错了什么?在此先感谢:-)

1 个答案:

答案 0 :(得分:1)

使用@escaping (Result) -> ()而不是@转义(EmailContentData) -> ()

static func getMessageList(index : Int, pageCount: Int, completion : @escaping (Result) -> ())
{
    DispatchQueue.global(qos: .userInteractive).async
        {
            let apiString : String = String(index) + "/" + String(pageCount)
            var objEmailContentData : EmailContentData? = EmailContentData.init(fromDictionary: NSDictionary())

            ApiHelper.sharedSession.post(Constant.API.messagelistApi + apiString, postData: NSDictionary(), methodtype: Constant.API.httpGet) { (isError, data, errorDescription) in
                guard let data = data else { return }
                do {
                    let result = EmailContentData.init(fromDictionary: data.value(forKey:"data") as! NSDictionary)
                    completion(Result.success(r: result))
                } catch let error {
                    completion(Result.failure(e: error))
                }
            }
    }
}