Optional Chaining क्या है
Optional chaining आपको किसी ऐसी चीज़ के बारे में सुरक्षित रूप से सवालों की एक chain पूछने देता है जो मौजूद नहीं हो सकती, crash होने के बजाय पहली "नहीं" पर चुपचाप रुकते हुए।
Syntax
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
// 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)
Login to try C/C++/Java/PHP code in the editor
Combining Chaining with Nil Coalescing
Optional chaining अक्सर ?? के साथ जोड़ा जाता है ताकि chain का कोई भी हिस्सा गायब होने पर एक default value दिया जाए।
उदाहरण: Combining Chaining with Nil Coalescing
// 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)
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. #}
आम गलतियां
- Optional chaining के
?.को force-unwrapping के!के साथ confuse करना -- chaining एक missing link पर crash होने के बजाय gracefullynilreturn करता है। - यह भूल जाना कि किसी optional chain का overall result हमेशा खुद एक optional है, भले ही final property न हो।
- किसी method call के through chain करने की कोशिश करना जो खुद एक optional return नहीं करता, और यह देखकर आश्चर्यचकित होना कि पूरे expression का result एक पहले के link के कारण अब भी optional है।
चैप्टर सारांश
?.एक property access करता है या एक method call करता है सिर्फ अगर optionalnilन हो।- अगर chain में कोई भी link
nilहो, पूरा expressionnilतक short-circuit हो जाता है। - किसी chain का overall result हमेशा एक optional में wrapped होता है, चाहे underlying property का type कुछ भी हो।
- Chaining nested optional structures access करने के लिए deeply nested
if letchecks से बचाता है।
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: