← Back to Swift Course | Chapter 5: Optionals | Lesson 7 of 8

Optional Chaining क्या है

Optional chaining आपको किसी ऐसी चीज़ के बारे में सुरक्षित रूप से सवालों की एक chain पूछने देता है जो मौजूद नहीं हो सकती, crash होने के बजाय पहली "नहीं" पर चुपचाप रुकते हुए।
Syntax
swift
let result = object.optionalProperty?.member
let result = object.optionalProperty?.member ?? defaultValue

Chaining Through an Optional Property

किसी optional के बाद ?. उपयोग करना आपको सुरक्षित रूप से इसकी एक property access करने देता है, अगर optional खुद खाली हो तो अपने आप nil return करते हुए।

उदाहरण: Chaining Through an Optional Property

markup
// Define a struct `Address`
struct Address {
    var city: String
}
// Define a struct `Person`
struct Person {
    var address: Address?
}
// Declare a constant `person`, set to `Person(address: Address(city: "Paris"))`
let person = Person(address: Address(city: "Paris"))
// Print `person.address?.city as Any` to the console
print(person.address?.city as Any)
// Declare a constant `personNoAddress`, set to `Person(address: nil)`
let personNoAddress = Person(address: nil)
// Print `personNoAddress.address?.city as Any` to the console
print(personNoAddress.address?.city as Any)

Combining Chaining with Nil Coalescing

Optional chaining अक्सर ?? के साथ जोड़ा जाता है ताकि chain का कोई भी हिस्सा गायब होने पर एक default value दिया जाए।

उदाहरण: Combining Chaining with Nil Coalescing

markup
// Define a struct `Address`
struct Address {
    var city: String
}
// Define a struct `Person`
struct Person {
    var address: Address?
}
// Declare a constant `person`, set to `Person(address: nil)`
let person = Person(address: nil)
// Declare a constant `city`, set to `person.address?.city ?? "Unknown city"`
let city = person.address?.city ?? "Unknown city"
// Print `city` to the console
print(city)
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. Optional chaining के ?. को force-unwrapping के ! के साथ confuse करना -- chaining एक missing link पर crash होने के बजाय gracefully nil return करता है।
  2. यह भूल जाना कि किसी optional chain का overall result हमेशा खुद एक optional है, भले ही final property न हो।
  3. किसी method call के through chain करने की कोशिश करना जो खुद एक optional return नहीं करता, और यह देखकर आश्चर्यचकित होना कि पूरे expression का result एक पहले के link के कारण अब भी optional है।
चैप्टर सारांश
  • ?. एक property access करता है या एक method call करता है सिर्फ अगर optional nil न हो।
  • अगर chain में कोई भी link nil हो, पूरा expression nil तक short-circuit हो जाता है।
  • किसी chain का overall result हमेशा एक optional में wrapped होता है, चाहे underlying property का type कुछ भी हो।
  • Chaining nested optional structures access करने के लिए deeply nested if let checks से बचाता है।
🔒

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.