← Back to Swift Course | Chapter 7: Closures | Lesson 4 of 6

Escaping Closures क्या हैं

एक escaping closure वह है जिसे बाद में उपयोग करने के लिए save किया गया है, इसे बनाने वाले function के पहले ही खत्म होकर चले जाने के बाद।
Syntax
swift
func functionName(_ completion: @escaping () -> Void) {
    storage.append(completion)   // stored and called later
}

func other(_ action: () -> Void) {
    action()                     // non-escaping: run before returning
}

Declaring an Escaping Closure Parameter

किसी closure parameter को @escaping चिह्नित करना इसे store (उदाहरण के लिए, एक array में) और बाद में invoke होने देता है, यहां तक कि जो function इसे receive करता है उसके return होने के बाद भी।

उदाहरण: Declaring an Escaping Closure Parameter

markup
// Declare a mutable variable `savedCompletions` (a `[() -> Void]`), initialized to `[]`
var savedCompletions: [() -> Void] = []
// Define the function `saveCompletion` taking `completion`
func saveCompletion(_ completion: @escaping () -> Void) {
    // Append `completion` to `savedCompletions`
    savedCompletions.append(completion)
}
saveCompletion { print("Completion 1 ran") }
saveCompletion { print("Completion 2 ran") }
// Loop over `savedCompletions`, binding each item to `completion`
for completion in savedCompletions {
    completion()
}

Non-Escaping by Default

Closures default रूप से non-escaping होते हैं, मतलब इन्हें function के return होने से पहले call किया जाना चाहिए -- यह ज़्यादा memory-efficient है और यही .map के closure argument को @escaping के बिना सुरक्षित बनाता है।

उदाहरण: Non-Escaping by Default

markup
// Define the function `runImmediately` taking `action`
func runImmediately(_ action: () -> Void) {
    // Print "About to run" to the console
    print("About to run")
    action()
    // Print "Finished running" to the console
    print("Finished running")
}
runImmediately {
    // Print "Running the action now" to the console
    print("Running the action now")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी closure parameter को @escaping चिह्नित करना भूल जाना जब यह एक property में stored हो या function के return होने के बाद call हो -- compiler इसे flag करेगा।
  2. यह मान लेना कि सभी closures को @escaping चाहिए; एक closure को इसकी ज़रूरत सिर्फ तब है जब यह function call से ज़्यादा जीए, जैसे बाद के लिए stored या asynchronously उपयोग किया गया।
  3. [weak self] उपयोग किए बिना एक stored escaping closure के अंदर self को strongly capture करके एक strong reference cycle बनाना।
चैप्टर सारांश
  • एक closure parameter को @escaping चिह्नित किया जाना चाहिए अगर यह stored हो या function के return होने के बाद call हो।
  • Non-escaping closures (default) को सिर्फ function call के दौरान ही उपयोग किया जाना चाहिए।
  • Escaping closures completion handlers और asynchronous callbacks के लिए आम हैं।
  • एक stored escaping closure में self capture करना एक retain cycle का जोखिम रखता है जब तक सावधानी से handle न किया जाए।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.