Ios 敏捷的如何从闭包返回函数
我有一个关于结案的问题Ios 敏捷的如何从闭包返回函数,ios,swift,closures,Ios,Swift,Closures,我有一个关于结案的问题 function AA(){ localPlayer.authenticateHandler{ //… if trigger { retrun } } dosomething() } 在上述代码中, 我想编写一段代码,当异步回调调用的authenticateHandler闭包满足触发器时,该代码希望返回到AA()函数 上面代码的结果是 当触发器发生时,只返回闭包,并在下面执行
function AA(){
localPlayer.authenticateHandler{
//…
if trigger {
retrun
}
}
dosomething()
}
在上述代码中,
我想编写一段代码,当异步回调调用的authenticateHandler闭包满足触发器时,该代码希望返回到AA()
函数
上面代码的结果是
当触发器
发生时,只返回闭包,并在下面执行dosomething()
方法
有什么办法吗
我有简短的英语技能,但感谢您的帮助。闭包不支持返回声明。而是在主线程内使用完成块来执行任务:
i、 e
函数AA(完成:@escaping(Result)->Void){
localPlayer.authenticateHandler{
//…
如果触发{
DispatchQueue.main.async{
完成(//任何返回值)
}
}
dosomething()
}
}
闭包不支持返回语句。
这样做。
在这里,我使用字符串比较来显示完成块将如何工作
func call() {
compareString(str1: "hey", str2: "hey") { isMatch in
if isMatch! { //Trigger
self.doSomething()
}
}
}
func compareString(str1:String,str2:String,completion:@escaping ((Bool?) -> Void)) {
if str1 == str2 {
completion(true)
}else {
completion(false)
}
}
func doSomething() {
}
这样不行。同步函数不能等待异步函数的结果。给AA一个回调闭包,并在authenticateHandler返回时调用它。闭包确实支持返回语句,但它们从闭包返回,而不是从包含方法返回。因此,您是否只想在触发器为true后执行dosomething()
,还是要在函数AA
中等待触发器为true
?如果触发器从未变为真
,该怎么办。我想在localPlayer.authenticateHandler
中等待,直到触发器为true
2。调用函数dosomething()
func call() {
compareString(str1: "hey", str2: "hey") { isMatch in
if isMatch! { //Trigger
self.doSomething()
}
}
}
func compareString(str1:String,str2:String,completion:@escaping ((Bool?) -> Void)) {
if str1 == str2 {
completion(true)
}else {
completion(false)
}
}
func doSomething() {
}