gpt4 book ai didi

ios - 'success' 中的成员 'Result' 产生类型为 'Result' 的结果,但上下文需要 'Result'

转载 作者:行者123 更新时间:2023-11-28 08:01:27 27 4
gpt4 key购买 nike

最近我刚刚将 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)
}
}
}

作为“Result”中的成员“success”出现错误会产生“Result”类型的结果,但上下文需要“Result”同一文件中的以下代码面临同样的问题

/**
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)
}
}
}

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

提前致谢

最佳答案

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)))
}
}
}

关于ios - 'success' 中的成员 'Result<AnyObject, NSError>' 产生类型为 'Result<Value, Error>' 的结果,但上下文需要 'Result<AnyObject, NSError>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46579703/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com