Super simple trailing closure syntax in SwiftHow do JavaScript closures work?What is the difference between a 'closure' and a 'lambda'?How can I remove a trailing newline in Python?JavaScript closure inside loops – simple practical exampleJavaScript closures vs. anonymous functions#pragma mark in Swift?Swift Beta performance: sorting arraysDont understand this Trailing ClosureSimple closure example with SwiftEquivalent of Swift trailing closure in C++
Will 700 more planes a day fly because of the Heathrow expansion?
What was the first story to feature the plot "the monsters were human all along"?
Adding command shortcuts to bin
Where can I go to avoid planes overhead?
Decoupling cap routing on a 4 layer PCB
Refinish or replace an old staircase
Are the Night's Watch still required?
What does 'made on' mean here?
What is a smasher?
In Russian, how do you idiomatically express the idea of the figurative "overnight"?
How can I roleplay a follower-type character when I as a player have a leader-type personality?
Why wasn't the Night King naked in S08E03?
How do LIGO and VIRGO know that a gravitational wave has its origin in a neutron star or a black hole?
Why does sound not move through a wall?
How to safely wipe a USB flash drive
How to adjust tikz picture so it fits to current size of a table cell?
Should I dumb down my writing in a foreign country?
Identifying characters
How did the Venus Express detect lightning?
I need a disease
Are Finitely generated modules over a ring also finitely generated over a subring containing the identity?
Is there an official reason for not adding a post-credits scene?
Should I decline this job offer that requires relocating to an area with high cost of living?
29er Road Tire?
Super simple trailing closure syntax in Swift
How do JavaScript closures work?What is the difference between a 'closure' and a 'lambda'?How can I remove a trailing newline in Python?JavaScript closure inside loops – simple practical exampleJavaScript closures vs. anonymous functions#pragma mark in Swift?Swift Beta performance: sorting arraysDont understand this Trailing ClosureSimple closure example with SwiftEquivalent of Swift trailing closure in C++
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to follow the example in the Swift docs for a trailing closure.
This is the function:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")//does not print
And I call it here.
print("about to call function")//prints ok
someFunctionThatTakesAClosure(closure:
print("we did what was in the function and can now do something else")//does not print
)
print("after calling function")//prints ok
The function, however, is not getting called. What is wrong with the above?
Here's the Apple example:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
// Here's how you call this function without using a trailing closure:
someFunctionThatTakesAClosure(closure:
// closure's body goes here )
ios swift closures trailing
add a comment |
I am trying to follow the example in the Swift docs for a trailing closure.
This is the function:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")//does not print
And I call it here.
print("about to call function")//prints ok
someFunctionThatTakesAClosure(closure:
print("we did what was in the function and can now do something else")//does not print
)
print("after calling function")//prints ok
The function, however, is not getting called. What is wrong with the above?
Here's the Apple example:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
// Here's how you call this function without using a trailing closure:
someFunctionThatTakesAClosure(closure:
// closure's body goes here )
ios swift closures trailing
A closure is just a value. Just like any other value, you can "ignore" its existence and nothing would happen. Making anything happen would require you to actually use the value.
– Alexander
Mar 23 at 0:27
add a comment |
I am trying to follow the example in the Swift docs for a trailing closure.
This is the function:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")//does not print
And I call it here.
print("about to call function")//prints ok
someFunctionThatTakesAClosure(closure:
print("we did what was in the function and can now do something else")//does not print
)
print("after calling function")//prints ok
The function, however, is not getting called. What is wrong with the above?
Here's the Apple example:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
// Here's how you call this function without using a trailing closure:
someFunctionThatTakesAClosure(closure:
// closure's body goes here )
ios swift closures trailing
I am trying to follow the example in the Swift docs for a trailing closure.
This is the function:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")//does not print
And I call it here.
print("about to call function")//prints ok
someFunctionThatTakesAClosure(closure:
print("we did what was in the function and can now do something else")//does not print
)
print("after calling function")//prints ok
The function, however, is not getting called. What is wrong with the above?
Here's the Apple example:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
// Here's how you call this function without using a trailing closure:
someFunctionThatTakesAClosure(closure:
// closure's body goes here )
ios swift closures trailing
ios swift closures trailing
asked Mar 22 at 23:58
user6631314user6631314
626413
626413
A closure is just a value. Just like any other value, you can "ignore" its existence and nothing would happen. Making anything happen would require you to actually use the value.
– Alexander
Mar 23 at 0:27
add a comment |
A closure is just a value. Just like any other value, you can "ignore" its existence and nothing would happen. Making anything happen would require you to actually use the value.
– Alexander
Mar 23 at 0:27
A closure is just a value. Just like any other value, you can "ignore" its existence and nothing would happen. Making anything happen would require you to actually use the value.
– Alexander
Mar 23 at 0:27
A closure is just a value. Just like any other value, you can "ignore" its existence and nothing would happen. Making anything happen would require you to actually use the value.
– Alexander
Mar 23 at 0:27
add a comment |
2 Answers
2
active
oldest
votes
Here is your example fixed:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")
// don't forget to call the closure
closure()
print("about to call function")
// call the function using trailing closure syntax
someFunctionThatTakesAClosure()
print("we did what was in the function and can now do something else")
print("after calling function")
Output:
about to call function
we do something here and then go back
we did what was in the function and can now do something else
after calling function
add a comment |
Docs isn't very clear in explanation you need
print("1")
someFunctionThatTakesAClosure() { // can be also someFunctionThatTakesAClosure without ()
print("3")
func someFunctionThatTakesAClosure(closure: () -> Void)
print("2")
/// do you job here and line blow will get you back
closure()
the trailing closure is meant for completions like when you do a network request and finally return the response like this
func someFunctionThatTakesAClosure(completion: @escaping ([String]) -> Void)
print("inside the function body")
Api.getData
completion(arr)
And to call
print("Before calling the function")
someFunctionThatTakesAClosure (arr) in
print("Inside the function callback / trailing closure " , arr)
print("After calling the function")
what you missed to read
Thanks, I saw that but read it as though you could use either one. Thx for clarifying! I upvoted answer.
– user6631314
Mar 23 at 2:34
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%2f55309237%2fsuper-simple-trailing-closure-syntax-in-swift%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is your example fixed:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")
// don't forget to call the closure
closure()
print("about to call function")
// call the function using trailing closure syntax
someFunctionThatTakesAClosure()
print("we did what was in the function and can now do something else")
print("after calling function")
Output:
about to call function
we do something here and then go back
we did what was in the function and can now do something else
after calling function
add a comment |
Here is your example fixed:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")
// don't forget to call the closure
closure()
print("about to call function")
// call the function using trailing closure syntax
someFunctionThatTakesAClosure()
print("we did what was in the function and can now do something else")
print("after calling function")
Output:
about to call function
we do something here and then go back
we did what was in the function and can now do something else
after calling function
add a comment |
Here is your example fixed:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")
// don't forget to call the closure
closure()
print("about to call function")
// call the function using trailing closure syntax
someFunctionThatTakesAClosure()
print("we did what was in the function and can now do something else")
print("after calling function")
Output:
about to call function
we do something here and then go back
we did what was in the function and can now do something else
after calling function
Here is your example fixed:
func someFunctionThatTakesAClosure(closure: () -> Void)
// function body goes here
print("we do something here and then go back")
// don't forget to call the closure
closure()
print("about to call function")
// call the function using trailing closure syntax
someFunctionThatTakesAClosure()
print("we did what was in the function and can now do something else")
print("after calling function")
Output:
about to call function
we do something here and then go back
we did what was in the function and can now do something else
after calling function
answered Mar 23 at 0:19
vacawamavacawama
101k15179206
101k15179206
add a comment |
add a comment |
Docs isn't very clear in explanation you need
print("1")
someFunctionThatTakesAClosure() { // can be also someFunctionThatTakesAClosure without ()
print("3")
func someFunctionThatTakesAClosure(closure: () -> Void)
print("2")
/// do you job here and line blow will get you back
closure()
the trailing closure is meant for completions like when you do a network request and finally return the response like this
func someFunctionThatTakesAClosure(completion: @escaping ([String]) -> Void)
print("inside the function body")
Api.getData
completion(arr)
And to call
print("Before calling the function")
someFunctionThatTakesAClosure (arr) in
print("Inside the function callback / trailing closure " , arr)
print("After calling the function")
what you missed to read
Thanks, I saw that but read it as though you could use either one. Thx for clarifying! I upvoted answer.
– user6631314
Mar 23 at 2:34
add a comment |
Docs isn't very clear in explanation you need
print("1")
someFunctionThatTakesAClosure() { // can be also someFunctionThatTakesAClosure without ()
print("3")
func someFunctionThatTakesAClosure(closure: () -> Void)
print("2")
/// do you job here and line blow will get you back
closure()
the trailing closure is meant for completions like when you do a network request and finally return the response like this
func someFunctionThatTakesAClosure(completion: @escaping ([String]) -> Void)
print("inside the function body")
Api.getData
completion(arr)
And to call
print("Before calling the function")
someFunctionThatTakesAClosure (arr) in
print("Inside the function callback / trailing closure " , arr)
print("After calling the function")
what you missed to read
Thanks, I saw that but read it as though you could use either one. Thx for clarifying! I upvoted answer.
– user6631314
Mar 23 at 2:34
add a comment |
Docs isn't very clear in explanation you need
print("1")
someFunctionThatTakesAClosure() { // can be also someFunctionThatTakesAClosure without ()
print("3")
func someFunctionThatTakesAClosure(closure: () -> Void)
print("2")
/// do you job here and line blow will get you back
closure()
the trailing closure is meant for completions like when you do a network request and finally return the response like this
func someFunctionThatTakesAClosure(completion: @escaping ([String]) -> Void)
print("inside the function body")
Api.getData
completion(arr)
And to call
print("Before calling the function")
someFunctionThatTakesAClosure (arr) in
print("Inside the function callback / trailing closure " , arr)
print("After calling the function")
what you missed to read
Docs isn't very clear in explanation you need
print("1")
someFunctionThatTakesAClosure() { // can be also someFunctionThatTakesAClosure without ()
print("3")
func someFunctionThatTakesAClosure(closure: () -> Void)
print("2")
/// do you job here and line blow will get you back
closure()
the trailing closure is meant for completions like when you do a network request and finally return the response like this
func someFunctionThatTakesAClosure(completion: @escaping ([String]) -> Void)
print("inside the function body")
Api.getData
completion(arr)
And to call
print("Before calling the function")
someFunctionThatTakesAClosure (arr) in
print("Inside the function callback / trailing closure " , arr)
print("After calling the function")
what you missed to read
edited Mar 23 at 0:46
answered Mar 23 at 0:09
Sh_KhanSh_Khan
49.6k51434
49.6k51434
Thanks, I saw that but read it as though you could use either one. Thx for clarifying! I upvoted answer.
– user6631314
Mar 23 at 2:34
add a comment |
Thanks, I saw that but read it as though you could use either one. Thx for clarifying! I upvoted answer.
– user6631314
Mar 23 at 2:34
Thanks, I saw that but read it as though you could use either one. Thx for clarifying! I upvoted answer.
– user6631314
Mar 23 at 2:34
Thanks, I saw that but read it as though you could use either one. Thx for clarifying! I upvoted answer.
– user6631314
Mar 23 at 2:34
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%2f55309237%2fsuper-simple-trailing-closure-syntax-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
A closure is just a value. Just like any other value, you can "ignore" its existence and nothing would happen. Making anything happen would require you to actually use the value.
– Alexander
Mar 23 at 0:27