Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength

Alamofire錯誤訊息:

FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

基本上就是API回傳為空值
造成Alamofire錯誤
來看一下回傳值 0 bytes ??
就可以知道根本沒傳回東西

Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength

Alamofire錯誤解決方法

responseJSON 加一個判斷
如果沒有response.result.value就跳出
避免閃退

Alamofire.upload(multipartFormData: { (multipartFormData) in
    for (key, value) in mParameters {
    // 這裡自己設定
}, to: url, headers: headers) { (encodingResult) in
    switch encodingResult {
    case .success(let upload, _, _):
        upload.responseJSON { response in
            if let value = response.result.value {
                success(JSON(value))
            }else {
                // 如果是空的就會過來 <-
                print(response)
            }
        }
    case .failure(let encodingError):
        failure(encodingError)
    }
}

Alamofire錯誤解法FAILURE: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)

Categorized in: