我有以下代码与Swift 2.2完美配合:
let request = NSMutableURLRequest(url: URL!.appendingPathComponent(result.path))
但是当我使上面的代码Swift 3.0兼容时,它没有按预期工作。下面是Swift 3.0代码,我在第import Alamofire
struct Communication {
static let foneKey = "fone"
static let emailKey = "email"
static let countryKey = "country"
enum Router: URLRequestConvertible {
/// Returns a URL request or throws if an `Error` was encountered.
///
/// - throws: An `Error` if the underlying `URLRequest` is `nil`.
///
/// - returns: A URL request.
public func asURLRequest() throws -> URLRequest {
return URLRequest
}
case init(String, String, String)
var URLRequest: URLRequest {
let result: (path: String, method: Alamofire.HTTPMethod, parameters: [String: AnyObject]) = {
switch self {
case .init(let foneKey, let email, let country):
let params = [foneKey: fone, emailKey: email, countryKey: country]
return ("Init", .post, params as [String : AnyObject])
}
}()
let URL = Foundation.URL(string: baseURLString)
let request = NSMutableURLRequest(url: URL!.appendingPathComponent(result.path))
let encoding = URLEncoding.methodDependent
request.httpMethod = result.method.rawValue
// request.URLRequest.HTTPMethod = result.method.rawValue
var request1: URLRequest?
do {
request1 = try encoding.encode(self, with: result.parameters)
} catch {
print("error in URLRequest encoding...")
}
return request1!
// return encoding.encode(request, parameters: result.parameters).0
}
}
}
行
func communicate() {
Alamofire.request(Communication.Router.init("8765432453", "xyz@abc.com", "1").URLRequest as URLRequestConvertible).responseJSON(options: [JSONSerialization.ReadingOptions.allowFragments, JSONSerialization.ReadingOptions.mutableContainers]) { (response) -> Void in
switch response.result {
case .success:
if let value = response.result.value {
// code handled
}
case .failure(let error):
print(error)
}
}
}
崩溃的原因是上述行的无限循环。
我想在创建Request对象时存在一些问题,但不确定我缺少什么。我尝试了几种替代方案而不是上面的代码,但没有一种方法有效。
有关详细信息,请参阅下面的方法:
create unique index unq_bookissuedReference
on Books(Book_id) where [status] = 'issued'
上述问题的可能解决方案是什么?一个有效的解决方案是创建Request单独为我工作。但我希望上面的通用解决方案目前在我的Swift 2.2应用程序中实现。