会员&#39;成功&#39;在&#39;结果<anyobject,nserror =“”>&#39;生成类型&#39;结果<值,错误=“”>&#39;的结果,但上下文需要&#39;结果<anyobject,nserror =“”>&#39;

时间:2017-10-05 07:01:09

标签: ios iphone swift swift3 alamofire

最近我刚刚将swift 2.3项目转换为3.2,alamofire也进行了转换,我收到了很多问题解决了大部分问题,现在我被困在给定的两个问题

alamofire

中的ResponseSerialization.swift文件中遇到了这个问题
/**
    Creates a response serializer that returns a JSON object constructed from the response data using 
    `NSJSONSerialization` with the specified reading options.

    - parameter options: The JSON serialization reading options. `.AllowFragments` by default.

    - returns: A JSON object response serializer.
*/
public static func JSONResponseSerializer(
    options: JSONSerialization.ReadingOptions = .allowFragments)
    -> ResponseSerializer<AnyObject, NSError>
{
    return ResponseSerializer { _, response, data, error in
        guard error == nil else { return .failure(error!) }

        if let response = response, response.statusCode == 204 { return .success(NSNull()) }

        guard let validData = data, validData.count > 0 else {
            let failureReason = "JSON could not be serialized. Input data was nil or zero length."
            let error = Error.error(code: .jsonSerializationFailed, failureReason: failureReason)
            return .failure(error)
        }

        do {
            let JSON = try JSONSerialization.jsonObject(with: validData, options: options)
            return .success(JSON) //getting error over here
        } catch {
            return .failure(error as NSError)
        }
    }
}

将错误视为会员&#39;成功&#39;在&#39;结果&#39;生成类型&#39;结果&#39;的结果,但上下文需要&#39;结果&#39; 同一文件中的以下代码面临同样的问题

/**
    Creates a response serializer that returns an object constructed from the response data using 
    `NSPropertyListSerialization` with the specified reading options.

    - parameter options: The property list reading options. `NSPropertyListReadOptions()` by default.

    - returns: A property list object response serializer.
*/
public static func propertyListResponseSerializer(
    options: PropertyListSerialization.ReadOptions = PropertyListSerialization.ReadOptions())
    -> ResponseSerializer<AnyObject, NSError>
{
    return ResponseSerializer { _, response, data, error in
        guard error == nil else { return .failure(error!) }

        if let response = response, response.statusCode == 204 { return .success(NSNull()) }

        guard let validData = data, validData.count > 0 else {
            let failureReason = "Property list could not be serialized. Input data was nil or zero length."
            let error = Error.error(code: .propertyListSerializationFailed, failureReason: failureReason)
            return .failure(error)
        }

        do {
            let plist = try PropertyListSerialization.propertyList(from: validData, options: options, format: nil)
            return .success(plist)
        } catch {
            return .failure(error as NSError)
        }
    }
}

到处搜索解决方案,但没有找到,请帮助我,

提前致谢

1 个答案:

答案 0 :(得分:0)

Swift-5简单替换此代码

public static func propertyListResponseSerializer(
    options: PropertyListSerialization.ReadOptions = [])
    -> DownloadResponseSerializer<Any>
{
    return DownloadResponseSerializer { _, response, fileURL, error in
        guard error == nil else { return .failure(error!) }

        guard let fileURL = fileURL else {
            return .failure(AFError.responseSerializationFailed(reason: .inputFileNil))
        }

        do {
            let data = try Data(contentsOf: fileURL)
            return Request.serializeResponsePropertyList(options: options, response: response, data: data, error: error)
        } catch {
            return .failure(AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL)))
        }
    }
}