Swift 2 - 使用 Alamofire 时函数不是 return Bool
Swift 2 - Function not return Bool when using Alamofire
我正在创建一个静态函数来检查用于注册的用户电子邮件是否正在使用中。如果电子邮件正在使用中,函数应该 return true
否则 false
.
我的代码总是 returning false
,似乎我使用的变量没有更新。
知道我做错了什么吗?为什么没有按预期工作?
class userInfo: NSObject {
static func userRegistration(email: String) -> Bool {
var emailIsavailable = false
Alamofire.request(.GET, "https://example/check_email.php?email=\(email)")
.responseString{ response in
if let responseValue = response.result.value {
print("Response String: \(responseValue)")
if responseValue == "email is available"{
print("email available")
emailIsavailable = true //emailIsavailable is not updated
}else{
print("email not available")
emailIsavailable = false //emailIsavailable is not updated
}
}
}
return emailIsavailable // returns always false
}
}
该函数将始终 return false
因为启动的网络请求是 异步的 (并且来自该请求的响应可以更改值emailIsavailable
)。
异步基本上意味着响应不会与该函数的执行同步接收。
我建议通过删除 return 类型 -> Bool
来更改该函数签名,并添加一个完成处理程序作为参数。然后您将在收到响应时调用该处理程序。
大致如下:
class userInfo: NSObject {
static func userRegistration(email: String, completionHandler: ((String) -> Void)) {
Alamofire.request(.GET, "https://example/check_email.php?email=\(email)")
.responseString{ response in
if let responseValue = response.result.value {
completionHandler(responseValue)
}
}
}
}
因为它 运行 在不同的线程中所以你不能 return 直接。您应该使用回调(或另一个调用块,闭包)。您应该编辑代码:
class userInfo: NSObject {
static func userRegistration(email: String, callBack : (emailIsavailable : Bool) -> Void) {
Alamofire.request(.GET, "https://example/check_email.php?email=\(email)")
.responseString{ response in
if let responseValue = response.result.value {
print("Response String: \(responseValue)")
if responseValue == "email is available"{
print("email available")
callBack(emailIsavailable: true)
}else{
print("email not available")
callBack(emailIsavailable: false)
}
}
}
}
}
你可以像这样打电话:
yourInstance.userRegistration("test") { (emailIsavailable) -> Void in
//you can get emailIsavaiable or something here
}
import Foundation
func foo() -> Bool {
var ret = false
// asynchronous code
// asyn will return immediately !!!
async { () -> () in
ret = true
print("print ret from completion block:", ret)
}
// ret value is false at this point
return ret
}
// this function is executed on its own queue. (aka Alamofire do)
func async(completion: ()->()) {
let queue = dispatch_queue_create("async", DISPATCH_QUEUE_CONCURRENT)
dispatch_async(queue) { () -> Void in
// do something and then
// execute completion
completion()
}
}
// will always return false !!
print("foo return:", foo())
/*
foo return: false
print ret from completion block: true
*/
你必须设计自己的同步机制。一般如果你需要某个函数执行的结果,那个函数必须同步执行。
我正在创建一个静态函数来检查用于注册的用户电子邮件是否正在使用中。如果电子邮件正在使用中,函数应该 return true
否则 false
.
我的代码总是 returning false
,似乎我使用的变量没有更新。
知道我做错了什么吗?为什么没有按预期工作?
class userInfo: NSObject {
static func userRegistration(email: String) -> Bool {
var emailIsavailable = false
Alamofire.request(.GET, "https://example/check_email.php?email=\(email)")
.responseString{ response in
if let responseValue = response.result.value {
print("Response String: \(responseValue)")
if responseValue == "email is available"{
print("email available")
emailIsavailable = true //emailIsavailable is not updated
}else{
print("email not available")
emailIsavailable = false //emailIsavailable is not updated
}
}
}
return emailIsavailable // returns always false
}
}
该函数将始终 return false
因为启动的网络请求是 异步的 (并且来自该请求的响应可以更改值emailIsavailable
)。
异步基本上意味着响应不会与该函数的执行同步接收。
我建议通过删除 return 类型 -> Bool
来更改该函数签名,并添加一个完成处理程序作为参数。然后您将在收到响应时调用该处理程序。
大致如下:
class userInfo: NSObject {
static func userRegistration(email: String, completionHandler: ((String) -> Void)) {
Alamofire.request(.GET, "https://example/check_email.php?email=\(email)")
.responseString{ response in
if let responseValue = response.result.value {
completionHandler(responseValue)
}
}
}
}
因为它 运行 在不同的线程中所以你不能 return 直接。您应该使用回调(或另一个调用块,闭包)。您应该编辑代码:
class userInfo: NSObject {
static func userRegistration(email: String, callBack : (emailIsavailable : Bool) -> Void) {
Alamofire.request(.GET, "https://example/check_email.php?email=\(email)")
.responseString{ response in
if let responseValue = response.result.value {
print("Response String: \(responseValue)")
if responseValue == "email is available"{
print("email available")
callBack(emailIsavailable: true)
}else{
print("email not available")
callBack(emailIsavailable: false)
}
}
}
}
}
你可以像这样打电话:
yourInstance.userRegistration("test") { (emailIsavailable) -> Void in
//you can get emailIsavaiable or something here
}
import Foundation
func foo() -> Bool {
var ret = false
// asynchronous code
// asyn will return immediately !!!
async { () -> () in
ret = true
print("print ret from completion block:", ret)
}
// ret value is false at this point
return ret
}
// this function is executed on its own queue. (aka Alamofire do)
func async(completion: ()->()) {
let queue = dispatch_queue_create("async", DISPATCH_QUEUE_CONCURRENT)
dispatch_async(queue) { () -> Void in
// do something and then
// execute completion
completion()
}
}
// will always return false !!
print("foo return:", foo())
/*
foo return: false
print ret from completion block: true
*/
你必须设计自己的同步机制。一般如果你需要某个函数执行的结果,那个函数必须同步执行。