Values को Capture करना
Capturing का मतलब है एक closure उन variables को याद रखता और पकड़े रहता है जहां यह बनाया गया था, भले ही code की वह जगह चलना खत्म कर चुकी हो।
Syntax
func makeFunction() -> () -> Type {
var captured = initialValue
return {
captured += 1 // closure keeps captured alive
return captured
}
}
Capturing a Variable
एक closure अपने आप किसी भी variable को capture कर लेता है जो यह अपने enclosing scope से उपयोग करता है, इन्हें उस scope खत्म होने के बाद भी alive और accessible रखते हुए।
उदाहरण: Capturing a Variable
// Define the function `makeIncrementer` taking `incrementAmount` and returning a `() -> Int`
func makeIncrementer(incrementAmount: Int) -> () -> Int {
// Declare a mutable variable `total`, initialized to `0`
var total = 0
// Declare a constant `incrementer` (a `() -> Int`); its value is the closure defined below
let incrementer: () -> Int = {
total += incrementAmount
// Return `total` from this function
return total
}
// Return `incrementer` from this function
return incrementer
}
// Declare a constant `incrementByTwo`, set to `makeIncrementer(incrementAmount: 2)`
let incrementByTwo = makeIncrementer(incrementAmount: 2)
// Print `incrementByTwo()` to the console
print(incrementByTwo())
// Print `incrementByTwo()` to the console
print(incrementByTwo())
// Print `incrementByTwo()` to the console
print(incrementByTwo())
Login to try C/C++/Java/PHP code in the editor
Independent Captures per Closure Instance
एक नया closure बनाने वाली हर call को अपना independent captured storage मिलता है, इसलिए अलग closures एक-दूसरे में interfere नहीं करते।
उदाहरण: Independent Captures per Closure Instance
// Define the function `makeIncrementer` taking `incrementAmount` and returning a `() -> Int`
func makeIncrementer(incrementAmount: Int) -> () -> Int {
// Declare a mutable variable `total`, initialized to `0`
var total = 0
// Return a closure that is defined in the block below
return {
total += incrementAmount
// Return `total` from this function
return total
}
}
// Declare a constant `counterA`, set to `makeIncrementer(incrementAmount: 1)`
let counterA = makeIncrementer(incrementAmount: 1)
// Declare a constant `counterB`, set to `makeIncrementer(incrementAmount: 10)`
let counterB = makeIncrementer(incrementAmount: 10)
// Print `counterA()` to the console
print(counterA())
// Print `counterB()` to the console
print(counterB())
// Print `counterA()` to the console
print(counterA())
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- यह मान लेना कि एक captured variable closure creation के moment पर copy होता है; Swift में, closures references capture करते हैं और बाद की mutations देखते हैं।
- यह देखकर आश्चर्यचकित होना कि उसी function से बनाए दो closures उसी captured variable की storage share करते हैं।
- यह न समझना कि एक long-lived closure में एक बड़े object को capture करना इसे उम्मीद से ज़्यादा लंबे समय तक memory में जीवित रख सकता है।
चैप्टर सारांश
- एक closure अपने आस-पास के context से variables और constants को reference से capture करता है।
- Capturing के कारण, एक closure इसके original scope खत्म होने के बाद भी एक variable access और modify कर सकता है।
- उसी context में बनाए गए कई closures उसी captured variable को share कर सकते हैं।
- यह capturing behavior वह है जो closures को counters और callbacks जैसी चीज़ों के लिए उपयोगी बनाता है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: