I have a Swift class from some sample code, and within it there is a property captureSession
declared like so:
private lazy var captureSession: AVCaptureSession = {
let session = AVCaptureSession()
guard
let backCamera = AVCaptureDevice.default(for: .video),
let input = try? AVCaptureDeviceInput(device: backCamera)
else { return session }
session.addInput(input)
return session
}()
I don't think captureSession
is a computed property, neither is it a closure. Then what is it?
captureSession
is lazy property but ={}()
is not regarding lazy initialization.
It is Setting a Default Property Value with a Closure or Function
. This is an example.
let titleLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.textColor = UIColor.textColor
label.font = UIFont(name: "HelveticaNeue-Medium", size: 14)
return label
}()
You can find more information at the end of this document.