简体   繁体   English

在Swift中关闭?

[英]Closure in Swift?

So I am reading the some code and I see a trailing closure, from my understanding and some googling, it seems trailing closure is used when you have a final parameter as a closure so you can pass it as a trailing closure. 因此,我正在阅读一些代码,并从我的理解和一些谷歌搜索中看到了尾随闭包,当您将最终参数作为闭包时,似乎使用了尾随闭包,因此您可以将其作为尾随闭包进行传递。

And this is what confuses me. 这就是让我困惑的地方。 I have a class A and a class B. Class B inherits from Class A. In Class A, there is a function C looks like this: 我有一个类A和一个类B。类B继承自类A。在类A中,有一个函数C看起来像这样:

func C(text1: String, text2: String) -> SomeOddType{...}

now in Class B, it overrides this function, but the body is like this: 现在在B类中,它重写了此功能,但主体如下所示:

override func C(text1: String, text2:String) -> SomeOddType{
 if let someVar = super.C(text:text1, text:text2){
  //some code that's not in the super method
  return someVar
  }
}

What does that do??? 那是做什么的? I am so confused. 我感到很困惑。 It doesn't have a closure as a parameter, and since it's already calling the super method, the code inside the override version is an add-on to the implementation? 它没有闭包作为参数,并且由于它已经在调用super方法,所以覆盖版本中的代码是实现的附加组件吗?

There is no trailing closure here, it's just the block of an if statement. 这里没有结尾的闭包,它只是if语句的块。 The expression super.C(text: text1, text: text2) is conditionally bound to the new constant someVar . 表达式super.C(text: text1, text: text2)有条件地绑定到新常量someVar If the conditional binding succeeds, it runs the "//some code that's not in the super method" block of code. 如果条件绑定成功,它将运行“ //某些不在超级方法中的代码”代码块。

super.C(text:text1, text:text2) returns SomeOddType , but it should be optional. super.C(text:text1, text:text2)返回SomeOddType ,但是它应该是可选的。 And it assigns to someVar 它分配给someVar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM