我是iOS编程的新手,我被困在SWIFT的闭包功能中。我已经提到了很多教程,并发现闭包是自编的代码,可以用于许多方面,例如。作为函数调用的参数,函数定义中的参数,变量。我在下面给出一个关于代码&问题的相关想法。如果我的理解错了,请帮助我。我知道我在很多方面都错了,所以请纠正我。在Swift中关闭?
1.1st部分
func TEST(text1:String,text2:String,flag: (S1:String,S2:String)->Bool)//In this line,I think,I am using flag is a closure which is passed as parameter in a function. And if so why doesn't it follow the standard closure syntax?
{
if flag(S1: text1, S2: text2) == true//I want to check the return type what flag closure gets when it compares the both string during function call. Why can't I write as if flag == true as flag is the name of the closure and ultimately refers to the return type of the closure?
{
print("they are equal")
}
else
{
//
}
}
第二部分
这部分是最麻烦的部分,真正让我困惑时,我调用该函数。在这里我也使用了相同的闭包。这里发生了什么?封口如何使用?它是捕捉价值还是其他东西?
TEST("heyy", text2: "heyy") { (S1, S2) -> Bool in
S1==S2
}
感谢您的关心。
我正在写一个答案,但决定在我投入更多时间之前阅读你的答案。这是+1 –