简体   繁体   中英

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:

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

now in Class B, it overrides this function, but the body is like this:

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?

There is no trailing closure here, it's just the block of an if statement. The expression super.C(text: text1, text: text2) is conditionally bound to the new constant 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. And it assigns to someVar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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