Why escaping closures require us to refer to self explicitly?
Apple's Swift documentation says that:
Marking a closure with @escaping means you have to refer to self
explicitly within the closure.
var completionHandlers: [() -> Void] =
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}
func someFunctionWithNonescapingClosure(closure: () -> Void) {
closure()
}
class SomeClass {
var x = 10
func doSomething() {
someFunctionWithEscapingClosure { self.x = 100 }
someFunctionWithNonescapingClosure { x = 200 }
}
}
But I am not able to understand the reason behind this?
In one of his Stanford Lectures (I don't exactly remember which one), Paul Hegarty said that Swift indicates the possibility of retention cycle by forcing you to write self. So is there a possibility of retention cycle while using escaping closures? Or is there any other reason why we have to explicitly refer to self within the closures marked as escaping?
And why in the other case (non escaping closures), there is no possibility of retention cycle?
Thanks in advance.
swift
add a comment |
Apple's Swift documentation says that:
Marking a closure with @escaping means you have to refer to self
explicitly within the closure.
var completionHandlers: [() -> Void] =
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}
func someFunctionWithNonescapingClosure(closure: () -> Void) {
closure()
}
class SomeClass {
var x = 10
func doSomething() {
someFunctionWithEscapingClosure { self.x = 100 }
someFunctionWithNonescapingClosure { x = 200 }
}
}
But I am not able to understand the reason behind this?
In one of his Stanford Lectures (I don't exactly remember which one), Paul Hegarty said that Swift indicates the possibility of retention cycle by forcing you to write self. So is there a possibility of retention cycle while using escaping closures? Or is there any other reason why we have to explicitly refer to self within the closures marked as escaping?
And why in the other case (non escaping closures), there is no possibility of retention cycle?
Thanks in advance.
swift
Please read this article, it describes the differences.
– vadian
Nov 25 '18 at 19:42
add a comment |
Apple's Swift documentation says that:
Marking a closure with @escaping means you have to refer to self
explicitly within the closure.
var completionHandlers: [() -> Void] =
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}
func someFunctionWithNonescapingClosure(closure: () -> Void) {
closure()
}
class SomeClass {
var x = 10
func doSomething() {
someFunctionWithEscapingClosure { self.x = 100 }
someFunctionWithNonescapingClosure { x = 200 }
}
}
But I am not able to understand the reason behind this?
In one of his Stanford Lectures (I don't exactly remember which one), Paul Hegarty said that Swift indicates the possibility of retention cycle by forcing you to write self. So is there a possibility of retention cycle while using escaping closures? Or is there any other reason why we have to explicitly refer to self within the closures marked as escaping?
And why in the other case (non escaping closures), there is no possibility of retention cycle?
Thanks in advance.
swift
Apple's Swift documentation says that:
Marking a closure with @escaping means you have to refer to self
explicitly within the closure.
var completionHandlers: [() -> Void] =
func someFunctionWithEscapingClosure(completionHandler: @escaping () -> Void) {
completionHandlers.append(completionHandler)
}
func someFunctionWithNonescapingClosure(closure: () -> Void) {
closure()
}
class SomeClass {
var x = 10
func doSomething() {
someFunctionWithEscapingClosure { self.x = 100 }
someFunctionWithNonescapingClosure { x = 200 }
}
}
But I am not able to understand the reason behind this?
In one of his Stanford Lectures (I don't exactly remember which one), Paul Hegarty said that Swift indicates the possibility of retention cycle by forcing you to write self. So is there a possibility of retention cycle while using escaping closures? Or is there any other reason why we have to explicitly refer to self within the closures marked as escaping?
And why in the other case (non escaping closures), there is no possibility of retention cycle?
Thanks in advance.
swift
swift
asked Nov 25 '18 at 19:20
DamonDamon
5181519
5181519
Please read this article, it describes the differences.
– vadian
Nov 25 '18 at 19:42
add a comment |
Please read this article, it describes the differences.
– vadian
Nov 25 '18 at 19:42
Please read this article, it describes the differences.
– vadian
Nov 25 '18 at 19:42
Please read this article, it describes the differences.
– vadian
Nov 25 '18 at 19:42
add a comment |
1 Answer
1
active
oldest
votes
Self is used to keep/capture the instance of the object you want to send the message. If you don’t use self, then the compiler wouldn’t know where to send the message
The stanford lecture is right, but why does this happen? Having self in a closure can turn into a reference cycle where the object is never let go. You can avoid this by using weak self or unknown self. You should only use weak self or unknown self however to avoid a strong reference cycle.
Let me know if that answers your question properly or if you need anymore help
1
Thanks, for your efforts. But I already know these things. My question is specifically related to the difference in behavior of escaping and non-escaping closures. Why we are required to use self.x in escaping closure and not in non-escaping closure. Thanks again. :)
– Damon
Nov 25 '18 at 19:41
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%2f53471017%2fwhy-escaping-closures-require-us-to-refer-to-self-explicitly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Self is used to keep/capture the instance of the object you want to send the message. If you don’t use self, then the compiler wouldn’t know where to send the message
The stanford lecture is right, but why does this happen? Having self in a closure can turn into a reference cycle where the object is never let go. You can avoid this by using weak self or unknown self. You should only use weak self or unknown self however to avoid a strong reference cycle.
Let me know if that answers your question properly or if you need anymore help
1
Thanks, for your efforts. But I already know these things. My question is specifically related to the difference in behavior of escaping and non-escaping closures. Why we are required to use self.x in escaping closure and not in non-escaping closure. Thanks again. :)
– Damon
Nov 25 '18 at 19:41
add a comment |
Self is used to keep/capture the instance of the object you want to send the message. If you don’t use self, then the compiler wouldn’t know where to send the message
The stanford lecture is right, but why does this happen? Having self in a closure can turn into a reference cycle where the object is never let go. You can avoid this by using weak self or unknown self. You should only use weak self or unknown self however to avoid a strong reference cycle.
Let me know if that answers your question properly or if you need anymore help
1
Thanks, for your efforts. But I already know these things. My question is specifically related to the difference in behavior of escaping and non-escaping closures. Why we are required to use self.x in escaping closure and not in non-escaping closure. Thanks again. :)
– Damon
Nov 25 '18 at 19:41
add a comment |
Self is used to keep/capture the instance of the object you want to send the message. If you don’t use self, then the compiler wouldn’t know where to send the message
The stanford lecture is right, but why does this happen? Having self in a closure can turn into a reference cycle where the object is never let go. You can avoid this by using weak self or unknown self. You should only use weak self or unknown self however to avoid a strong reference cycle.
Let me know if that answers your question properly or if you need anymore help
Self is used to keep/capture the instance of the object you want to send the message. If you don’t use self, then the compiler wouldn’t know where to send the message
The stanford lecture is right, but why does this happen? Having self in a closure can turn into a reference cycle where the object is never let go. You can avoid this by using weak self or unknown self. You should only use weak self or unknown self however to avoid a strong reference cycle.
Let me know if that answers your question properly or if you need anymore help
answered Nov 25 '18 at 19:33
cevalloacevalloa
1024
1024
1
Thanks, for your efforts. But I already know these things. My question is specifically related to the difference in behavior of escaping and non-escaping closures. Why we are required to use self.x in escaping closure and not in non-escaping closure. Thanks again. :)
– Damon
Nov 25 '18 at 19:41
add a comment |
1
Thanks, for your efforts. But I already know these things. My question is specifically related to the difference in behavior of escaping and non-escaping closures. Why we are required to use self.x in escaping closure and not in non-escaping closure. Thanks again. :)
– Damon
Nov 25 '18 at 19:41
1
1
Thanks, for your efforts. But I already know these things. My question is specifically related to the difference in behavior of escaping and non-escaping closures. Why we are required to use self.x in escaping closure and not in non-escaping closure. Thanks again. :)
– Damon
Nov 25 '18 at 19:41
Thanks, for your efforts. But I already know these things. My question is specifically related to the difference in behavior of escaping and non-escaping closures. Why we are required to use self.x in escaping closure and not in non-escaping closure. Thanks again. :)
– Damon
Nov 25 '18 at 19:41
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%2f53471017%2fwhy-escaping-closures-require-us-to-refer-to-self-explicitly%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
Please read this article, it describes the differences.
– vadian
Nov 25 '18 at 19:42