What does Swift mean by {}()?

Asked 2 years ago, Updated 2 years ago, 32 views

Swift has the following code:

 var videoBitrateLabel:UILabel={
    let label: UILabel=UILabel()
    label.textColor=UIColor.whiteColor()
    return label}()

I will addSubview this instance later.

view.addSubview(videoBitrateLabel)

At a glance, I can understand that it is like this, but please tell me the meaning (effect?) of {} and ().
Also, how can I find out if I want to google it?

swift

2022-09-30 17:39

1 Answers

It will be the same as I wrote below.
Closure expression with no arguments, return value UILabel, and } after in.
You can omit ( ) - > UILabelin because of the type inference feature in swift.
Depending on the number of arguments and return values, you may not be able to omit them.
The last ( ) runs the closure formula.

var videoBitrateLabel:UILabel={()->UILabel in 
    let label: UILabel=UILabel()
    label.textColor=UIColor.whiteColor()
    return label
}()


2022-09-30 17:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.