【发布时间】:2018-03-16 17:21:33
【问题描述】:
最近我刚刚将 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)
}
}
}
在“结果”中将错误作为成员“成功”生成“结果”类型的结果,但上下文需要“结果” 同一文件中的以下代码面临同样的问题
/**
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)
}
}
}
到处寻找解决方案,但没有找到,请帮助我,
提前致谢
【问题讨论】:
-
我没有转换,是xcode自动完成的
-
然后删除它并添加 swift 3.2 兼容版本的 Alamofire。
-
我收到了这样的错误,因为我在
.success()中传递了 Optional
标签: ios iphone swift swift3 alamofire