Anna
Anna

Reputation: 571

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?

Upvotes: 0

Views: 180

Answers (2)

Alexander
Alexander

Reputation: 63395

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.

Upvotes: 3

mqz.kim
mqz.kim

Reputation: 1049

super.C(text:text1, text:text2) returns SomeOddType, but it should be optional. And it assigns to someVar

Upvotes: 0

Related Questions