← Back to Swift Course | Chapter 3: Control Flow | Lesson 8 of 8

Pattern Matching क्या है

Pattern matching एक powerful तरीका है यह जांचने का कि कोई value एक particular shape से fit होती है, और साथ ही इसके pieces निकालने का।
Syntax
swift
switch (a, b) {
case (0, let y):       // tuple pattern
    // code
case 1...10:          // range pattern
    // code
default: break
}

if case pattern = value { }

Matching Tuples

एक switch किसी tuple की shape के खिलाफ match कर सकता है, और case let bindings आपको इसके individual components निकालने देते हैं।

उदाहरण: Matching Tuples

markup
// Declare a constant `coordinate`, set to `(0, 5)`
let coordinate = (0, 5)
// Switch on `coordinate` and branch by case
switch coordinate {
// Case: `(0, 0)`
case (0, 0):
    // Print "Origin" to the console
    print("Origin")
// Case: `(0, let y)`
case (0, let y):
    // Print "On the y-axis at \(y)" to the console
    print("On the y-axis at \(y)")
// Case: `(let x, 0)`
case (let x, 0):
    // Print "On the x-axis at \(x)" to the console
    print("On the x-axis at \(x)")
// Case: `let (x, y)`
case let (x, y):
    // Print "At (\(x), \(y))" to the console
    print("At (\(x), \(y))")
}

Matching Ranges

एक case किसी value को एक numeric range के खिलाफ match कर सकता है, जो values को categories में group करने के लिए उपयोगी है।

उदाहरण: Matching Ranges

markup
// Declare a constant `score`, set to `85`
let score = 85
// Switch on `score` and branch by case
switch score {
// Case: `90...100`
case 90...100:
    // Print "Grade: A" to the console
    print("Grade: A")
// Case: `80..<90`
case 80..<90:
    // Print "Grade: B" to the console
    print("Grade: B")
default:
    // Print "Grade: C or below" to the console
    print("Grade: C or below")
}

if case for Single-Value Matching

जब आपको सिर्फ एक पूरे switch के बिना एक pattern test करना हो, if case एक lightweight alternative प्रदान करता है।

उदाहरण: if case for Single-Value Matching

markup
// Declare a constant `value` (a `Int?`), set to `7`
let value: Int? = 7
// Check whether `case .some(let number) = value`
if case .some(let number) = value {
    // Print "Got a number: \(number)" to the console
    print("Got a number: \(number)")
}
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. यह सोचना कि pattern matching सिर्फ enums पर switch के लिए है; यह tuples, ranges, और optional binding के साथ भी काम करता है।
  2. यह भूल जाना कि एक case let pattern नए local constants bind करता है जो सिर्फ उस case की body के अंदर मौजूद हैं।
  3. यह नज़रअंदाज़ करना कि case order मायने रखता है -- एक switch में पहले एक broader pattern बाद में लिखे गए एक ज़्यादा specific को shadow कर देगा।
चैप्टर सारांश
  • Swift में Pattern matching switch, if case, और for case constructs में दिखता है।
  • Tuple patterns कई values match कर सकते हैं और एक साथ इन्हें destructure कर सकते हैं।
  • Range patterns (case 1...5) एक value को match करते हैं जो उस range के अंदर आता है।
  • if case आपको एक पूरे switch statement के बिना एक single value pattern-match करने देता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.