What is the nature of a class property when declared with “= {}()” in Swift?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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?
ios swift avcapturesession
add a comment |
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?
ios swift avcapturesession
add a comment |
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?
ios swift avcapturesession
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?
ios swift avcapturesession
ios swift avcapturesession
edited Nov 22 '18 at 8:28
Shruti Thombre
8292824
8292824
asked Nov 22 '18 at 7:28
Gang FangGang Fang
12518
12518
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
In the doc it says: "Note that the closure’s end curly brace is followed by an empty pair of parentheses. This tells Swift to execute the closure immediately.", that is what I missed!
– Gang Fang
Nov 22 '18 at 11:07
add a comment |
This is Lazy initialisation. It is often used when initial value is relatively expensive to create. So you create value when you sure you need it. So captureSession
will be created when you access it first time and then stored in captureSession
variable.
The syntax ={}()
depicts a closure (anonymous function) which is called when you access your property. This closure return Type is AVCaptureSession
.
Other words: Using lazy var a: SomeType = { ... }()
you postpone object a
creation until you really need it. When you access it first time, the variable a
will take a result of the closure.
I like the explanation written here, Official documentation can also be helpful.
1
The= { ... }()
syntax is not part of the lazy initialization in any way though. You can use it even for normal variables and on the other hand, you don't need it forlazy
variables.
– Sulthan
Nov 22 '18 at 9:05
@Sulthan, the= {...}()
is just a way to write closure result assigned to variable. I was answering in the context of the question.
– fewlinesofcode
Nov 22 '18 at 9:06
add a comment |
Lazy properties allow you to create certain parts of a Swift type when needed, rather than doing it as part of its initialization process. This can be useful in order to avoid optionals, or to improve performance when certain properties might be expensive to create. It can also help with keeping initializers more clean, since you can defer some of the setup of your types until later in their lifecycle.
In simple words captureSession instance won't be created until you access it the first time, and after instantiation will return the same instance everytime.
The advantage of this approach is that you can keep both the property declaration and its setup in one place.
Here's a nice article on lazy initialization: Using lazy properties in Swift
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425842%2fwhat-is-the-nature-of-a-class-property-when-declared-with-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
In the doc it says: "Note that the closure’s end curly brace is followed by an empty pair of parentheses. This tells Swift to execute the closure immediately.", that is what I missed!
– Gang Fang
Nov 22 '18 at 11:07
add a comment |
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.
In the doc it says: "Note that the closure’s end curly brace is followed by an empty pair of parentheses. This tells Swift to execute the closure immediately.", that is what I missed!
– Gang Fang
Nov 22 '18 at 11:07
add a comment |
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.
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.
edited Nov 22 '18 at 11:17
savepopulation
7,94743753
7,94743753
answered Nov 22 '18 at 8:02
Emre ÖzdilEmre Özdil
328510
328510
In the doc it says: "Note that the closure’s end curly brace is followed by an empty pair of parentheses. This tells Swift to execute the closure immediately.", that is what I missed!
– Gang Fang
Nov 22 '18 at 11:07
add a comment |
In the doc it says: "Note that the closure’s end curly brace is followed by an empty pair of parentheses. This tells Swift to execute the closure immediately.", that is what I missed!
– Gang Fang
Nov 22 '18 at 11:07
In the doc it says: "Note that the closure’s end curly brace is followed by an empty pair of parentheses. This tells Swift to execute the closure immediately.", that is what I missed!
– Gang Fang
Nov 22 '18 at 11:07
In the doc it says: "Note that the closure’s end curly brace is followed by an empty pair of parentheses. This tells Swift to execute the closure immediately.", that is what I missed!
– Gang Fang
Nov 22 '18 at 11:07
add a comment |
This is Lazy initialisation. It is often used when initial value is relatively expensive to create. So you create value when you sure you need it. So captureSession
will be created when you access it first time and then stored in captureSession
variable.
The syntax ={}()
depicts a closure (anonymous function) which is called when you access your property. This closure return Type is AVCaptureSession
.
Other words: Using lazy var a: SomeType = { ... }()
you postpone object a
creation until you really need it. When you access it first time, the variable a
will take a result of the closure.
I like the explanation written here, Official documentation can also be helpful.
1
The= { ... }()
syntax is not part of the lazy initialization in any way though. You can use it even for normal variables and on the other hand, you don't need it forlazy
variables.
– Sulthan
Nov 22 '18 at 9:05
@Sulthan, the= {...}()
is just a way to write closure result assigned to variable. I was answering in the context of the question.
– fewlinesofcode
Nov 22 '18 at 9:06
add a comment |
This is Lazy initialisation. It is often used when initial value is relatively expensive to create. So you create value when you sure you need it. So captureSession
will be created when you access it first time and then stored in captureSession
variable.
The syntax ={}()
depicts a closure (anonymous function) which is called when you access your property. This closure return Type is AVCaptureSession
.
Other words: Using lazy var a: SomeType = { ... }()
you postpone object a
creation until you really need it. When you access it first time, the variable a
will take a result of the closure.
I like the explanation written here, Official documentation can also be helpful.
1
The= { ... }()
syntax is not part of the lazy initialization in any way though. You can use it even for normal variables and on the other hand, you don't need it forlazy
variables.
– Sulthan
Nov 22 '18 at 9:05
@Sulthan, the= {...}()
is just a way to write closure result assigned to variable. I was answering in the context of the question.
– fewlinesofcode
Nov 22 '18 at 9:06
add a comment |
This is Lazy initialisation. It is often used when initial value is relatively expensive to create. So you create value when you sure you need it. So captureSession
will be created when you access it first time and then stored in captureSession
variable.
The syntax ={}()
depicts a closure (anonymous function) which is called when you access your property. This closure return Type is AVCaptureSession
.
Other words: Using lazy var a: SomeType = { ... }()
you postpone object a
creation until you really need it. When you access it first time, the variable a
will take a result of the closure.
I like the explanation written here, Official documentation can also be helpful.
This is Lazy initialisation. It is often used when initial value is relatively expensive to create. So you create value when you sure you need it. So captureSession
will be created when you access it first time and then stored in captureSession
variable.
The syntax ={}()
depicts a closure (anonymous function) which is called when you access your property. This closure return Type is AVCaptureSession
.
Other words: Using lazy var a: SomeType = { ... }()
you postpone object a
creation until you really need it. When you access it first time, the variable a
will take a result of the closure.
I like the explanation written here, Official documentation can also be helpful.
edited Nov 22 '18 at 7:41
answered Nov 22 '18 at 7:35
fewlinesofcodefewlinesofcode
2,1371820
2,1371820
1
The= { ... }()
syntax is not part of the lazy initialization in any way though. You can use it even for normal variables and on the other hand, you don't need it forlazy
variables.
– Sulthan
Nov 22 '18 at 9:05
@Sulthan, the= {...}()
is just a way to write closure result assigned to variable. I was answering in the context of the question.
– fewlinesofcode
Nov 22 '18 at 9:06
add a comment |
1
The= { ... }()
syntax is not part of the lazy initialization in any way though. You can use it even for normal variables and on the other hand, you don't need it forlazy
variables.
– Sulthan
Nov 22 '18 at 9:05
@Sulthan, the= {...}()
is just a way to write closure result assigned to variable. I was answering in the context of the question.
– fewlinesofcode
Nov 22 '18 at 9:06
1
1
The
= { ... }()
syntax is not part of the lazy initialization in any way though. You can use it even for normal variables and on the other hand, you don't need it for lazy
variables.– Sulthan
Nov 22 '18 at 9:05
The
= { ... }()
syntax is not part of the lazy initialization in any way though. You can use it even for normal variables and on the other hand, you don't need it for lazy
variables.– Sulthan
Nov 22 '18 at 9:05
@Sulthan, the
= {...}()
is just a way to write closure result assigned to variable. I was answering in the context of the question.– fewlinesofcode
Nov 22 '18 at 9:06
@Sulthan, the
= {...}()
is just a way to write closure result assigned to variable. I was answering in the context of the question.– fewlinesofcode
Nov 22 '18 at 9:06
add a comment |
Lazy properties allow you to create certain parts of a Swift type when needed, rather than doing it as part of its initialization process. This can be useful in order to avoid optionals, or to improve performance when certain properties might be expensive to create. It can also help with keeping initializers more clean, since you can defer some of the setup of your types until later in their lifecycle.
In simple words captureSession instance won't be created until you access it the first time, and after instantiation will return the same instance everytime.
The advantage of this approach is that you can keep both the property declaration and its setup in one place.
Here's a nice article on lazy initialization: Using lazy properties in Swift
add a comment |
Lazy properties allow you to create certain parts of a Swift type when needed, rather than doing it as part of its initialization process. This can be useful in order to avoid optionals, or to improve performance when certain properties might be expensive to create. It can also help with keeping initializers more clean, since you can defer some of the setup of your types until later in their lifecycle.
In simple words captureSession instance won't be created until you access it the first time, and after instantiation will return the same instance everytime.
The advantage of this approach is that you can keep both the property declaration and its setup in one place.
Here's a nice article on lazy initialization: Using lazy properties in Swift
add a comment |
Lazy properties allow you to create certain parts of a Swift type when needed, rather than doing it as part of its initialization process. This can be useful in order to avoid optionals, or to improve performance when certain properties might be expensive to create. It can also help with keeping initializers more clean, since you can defer some of the setup of your types until later in their lifecycle.
In simple words captureSession instance won't be created until you access it the first time, and after instantiation will return the same instance everytime.
The advantage of this approach is that you can keep both the property declaration and its setup in one place.
Here's a nice article on lazy initialization: Using lazy properties in Swift
Lazy properties allow you to create certain parts of a Swift type when needed, rather than doing it as part of its initialization process. This can be useful in order to avoid optionals, or to improve performance when certain properties might be expensive to create. It can also help with keeping initializers more clean, since you can defer some of the setup of your types until later in their lifecycle.
In simple words captureSession instance won't be created until you access it the first time, and after instantiation will return the same instance everytime.
The advantage of this approach is that you can keep both the property declaration and its setup in one place.
Here's a nice article on lazy initialization: Using lazy properties in Swift
edited Nov 22 '18 at 7:43
answered Nov 22 '18 at 7:37
Daniyal RazaDaniyal Raza
27423
27423
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425842%2fwhat-is-the-nature-of-a-class-property-when-declared-with-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown