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

Closure का Shorthand Syntax

Closure shorthand Swift का तरीका है आपको एक closure कम words में लिखने देने का एक बार जब context पहले से चीज़ें obvious बना दे।
Syntax
swift
array.sorted { a, b in a < b }   // inferred types, implicit return
array.map { $0 * 2 }            // shorthand names $0, $1
array.sorted(by: >)             // operator as closure

Type Inference and Implicit Return

जब Swift पहले से context से किसी closure का expected type जानता है, closure explicit types और single expression के लिए return keyword छोड़ सकता है।

उदाहरण: Type Inference and Implicit Return

markup
// Declare a constant `numbers`, set to `[4, 2, 7, 1]`
let numbers = [4, 2, 7, 1]
// Declare a constant `sorted`, set to `numbers.sorted { a, b in a < b }`
let sorted = numbers.sorted { a, b in a < b }
// Print `sorted` to the console
print(sorted)

Shorthand Argument Names

Swift अपने आप $0, $1, वगैरह किसी closure के parameters के नाम के रूप में प्रदान करता है, आपको छोटे closures के लिए इन्हें नाम देने से पूरी तरह बचने देते हुए।

उदाहरण: Shorthand Argument Names

markup
// Declare a constant `numbers`, set to `[4, 2, 7, 1]`
let numbers = [4, 2, 7, 1]
// Declare a constant `doubled`, set to `numbers.map { $0 * 2 }`
let doubled = numbers.map { $0 * 2 }
// Declare a constant `sorted`, set to `numbers.sorted { $0 < $1 }`
let sorted = numbers.sorted { $0 < $1 }
// Print `doubled` to the console
print(doubled)
// Print `sorted` to the console
print(sorted)

Passing an Operator as a Closure

जब किसी operator का signature expected closure type से बिल्कुल मेल खाता है, इसे एक closure लिखने के बजाय सीधे नाम से pass किया जा सकता है।

उदाहरण: Passing an Operator as a Closure

markup
// Declare a constant `numbers`, set to `[4, 2, 7, 1]`
let numbers = [4, 2, 7, 1]
// Declare a constant `descending`, set to `numbers.sorted(by: >)`
let descending = numbers.sorted(by: >)
// Print `descending` to the console
print(descending)
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. Complex logic वाले closures के लिए $0, $1 shorthand overuse करना, readability नुकसान पहुंचाते हुए; shorthand छोटे, simple closures के लिए सबसे अच्छा है।
  2. एक single-expression closure body में return लिखना; यह वहां implicit और optional है लेकिन multi-statement closures में ज़रूरी है।
  3. यह भूल जाना कि + या > जैसे operator functions को सीधे एक closure के रूप में pass किया जा सकता है जब उनका signature बिल्कुल मेल खाता हो।
चैप्टर सारांश
  • Swift किसी known function signature को pass किए closure में parameter और return types infer कर सकता है।
  • $0, $1, वगैरह किसी closure के parameters को positionally refer करते हैं उन्हें नाम दिए बिना।
  • एक single-expression closure body return keyword छोड़ सकता है।
  • +, <, या > जैसे operators को सीधे एक two-parameter function type से मेल खाने वाले closures के रूप में pass किया जा सकता है।
🔒

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.