使用 Swift(请求)的 Alamofire 和 PromiseKit 的 Void 错误
Error on Void with Alamofire and PromiseKit with Swift (Request)
在我的最后一个项目中,我一直在使用 alamofire 和 promisekit 连接到我的 Web 服务,现在我有了两者的新版本,当我创建请求时,我得到了这个错误
Cannot convert value of type 'DataRequest' to closure result type 'Void'
当我尝试这样做时会发生这种情况:
func CallOne() -> Promise<[String: AnyObject]>{
return Promise{
fullfil,reject -> Void in
return Alamofire.request(
url_api,
method: .get).responseJSON{
response in
switch(response.result){
case .success(let res):
let data = res as? [String: AnyObject]
fullfil(data!)
case .failure(let error):
reject(error)
}
}
}
}
当我尝试使用它时:
func CallTwo(email: String, phone: String) -> Promise<[String: AnyObject]>{
let params =
["email": email, "phone": phone]
return Promise{
fullfil,reject -> Void in
return Alamofire.upload(
multipartFormData: { MultipartFormData in
for (key, value) in params {
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to: url_api, method: .post) { (result) in
switch (result){
case .success(let upload, _, _):
upload.responseJSON { response in
switch(response.result){
case .success(let res):
let data = res as? [String: AnyObject]
fullfil(data!)
case .failure(let error):
reject(error)
}
}
case .failure(let encodingError):
print(encodingError)
}
}
}
}
我收到这个错误 Unable to infer closure type in the current context
我该如何解决?提前致谢
我用
解决了这个问题
seal in
func CallOne() -> Promise<[String: AnyObject]>{
return Promise{
seal in
return Alamofire.request(
url_api,
method: .get).responseJSON{
response in
switch(response.result){
case .success(let res):
let data = res as? [String: AnyObject]
seal.fulfill(data!)
case .failure(let error):
seal.reject(error)
}
}
}
}
func CallTwo(email: String, phone: String) -> Promise<[String: AnyObject]>{
let params =
["email": email, "phone": phone]
return Promise{
seal in
return Alamofire.upload(
multipartFormData: { MultipartFormData in
for (key, value) in params {
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to: url_api, method: .post) { (result) in
switch (result){
case .success(let upload, _, _):
upload.responseJSON { response in
switch(response.result){
case .success(let res):
let data = res as? [String: AnyObject]
seal.fulfill(data!)
case .failure(let error):
seal.reject(error)
}
}
case .failure(let encodingError):
print(encodingError)
}
}
}
}
在我的最后一个项目中,我一直在使用 alamofire 和 promisekit 连接到我的 Web 服务,现在我有了两者的新版本,当我创建请求时,我得到了这个错误
Cannot convert value of type 'DataRequest' to closure result type 'Void'
当我尝试这样做时会发生这种情况:
func CallOne() -> Promise<[String: AnyObject]>{
return Promise{
fullfil,reject -> Void in
return Alamofire.request(
url_api,
method: .get).responseJSON{
response in
switch(response.result){
case .success(let res):
let data = res as? [String: AnyObject]
fullfil(data!)
case .failure(let error):
reject(error)
}
}
}
}
当我尝试使用它时:
func CallTwo(email: String, phone: String) -> Promise<[String: AnyObject]>{
let params =
["email": email, "phone": phone]
return Promise{
fullfil,reject -> Void in
return Alamofire.upload(
multipartFormData: { MultipartFormData in
for (key, value) in params {
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to: url_api, method: .post) { (result) in
switch (result){
case .success(let upload, _, _):
upload.responseJSON { response in
switch(response.result){
case .success(let res):
let data = res as? [String: AnyObject]
fullfil(data!)
case .failure(let error):
reject(error)
}
}
case .failure(let encodingError):
print(encodingError)
}
}
}
}
我收到这个错误 Unable to infer closure type in the current context
我该如何解决?提前致谢
我用
解决了这个问题seal in
func CallOne() -> Promise<[String: AnyObject]>{
return Promise{
seal in
return Alamofire.request(
url_api,
method: .get).responseJSON{
response in
switch(response.result){
case .success(let res):
let data = res as? [String: AnyObject]
seal.fulfill(data!)
case .failure(let error):
seal.reject(error)
}
}
}
}
func CallTwo(email: String, phone: String) -> Promise<[String: AnyObject]>{
let params =
["email": email, "phone": phone]
return Promise{
seal in
return Alamofire.upload(
multipartFormData: { MultipartFormData in
for (key, value) in params {
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to: url_api, method: .post) { (result) in
switch (result){
case .success(let upload, _, _):
upload.responseJSON { response in
switch(response.result){
case .success(let res):
let data = res as? [String: AnyObject]
seal.fulfill(data!)
case .failure(let error):
seal.reject(error)
}
}
case .failure(let encodingError):
print(encodingError)
}
}
}
}