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

Implicitly Unwrapped Optionals क्या हैं

एक implicitly unwrapped optional एक value है जो technically एक optional box है, लेकिन Swift आपको इसे हर बार खोले बिना सीधे उपयोग करने देता है, यह भरोसा करते हुए कि setup के बाद इसके अंदर कुछ होगा।
Syntax
swift
var name: Type! = value    // implicitly unwrapped
name.member                // used without ! or ?
name = nil                 // accessing it now crashes

Declaring an Implicitly Unwrapped Optional

एक implicitly unwrapped optional ? के बजाय ! से declare किया जाता है, इसकी value को हर बार manual unwrapping के बिना सीधे उपयोग होने देते हुए, एक बार यह assigned हो जाए।

उदाहरण: Declaring an Implicitly Unwrapped Optional

markup
// Declare a mutable variable `username` (a `String!`), initialized to `"swiftdev"`
var username: String! = "swiftdev"
// Print "Username: \(username)" to the console
print("Username: \(username)")
// Print `username.uppercased()` to the console
print(username.uppercased())

Still Crashes When nil

भले ही इसे सीधे उपयोग किया जा सकता है, एक implicitly unwrapped optional अब भी program को crash करता है अगर इसे nil रखते हुए access किया जाए, इसलिए इसे सिर्फ तब उपयोग करना चाहिए जब एक value guaranteed हो।

Note: Type! को force-unwrapping जैसी ही सावधानी से treat करें -- इसे सिर्फ तब उपयोग करें जब एक nil value एक genuine programming error indicate करे।

उदाहरण: Still Crashes When nil

markup
// Declare a mutable variable `configValue` (a `Int!`), initialized to `100`
var configValue: Int! = 100
// Check whether `configValue != nil`
if configValue != nil {
    // Print "Config is set to \(configValue!)" to the console
    print("Config is set to \(configValue!)")
}
configValue = nil
// Print "Config is now nil, checking before use avoids a crash" to the console
print("Config is now nil, checking before use avoids a crash")
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. String! को regular optionals के general substitute के रूप में उपयोग करना; इसे उन cases के लिए reserved रखना चाहिए जहां एक value nil से शुरू होती है लेकिन उपयोग से पहले set होने की guarantee है।
  2. यह भूल जाना कि एक implicitly unwrapped optional अब भी runtime पर crash करता है, बिल्कुल force-unwrapping की तरह, अगर इसे असल में nil रखते हुए access किया जाए।
  3. यह न समझना कि Type! को अब भी if let से जांचा जा सकता है या nil से compare किया जा सकता है बिल्कुल एक normal optional की तरह -- यह पूरी तरह अलग mechanism नहीं है।
चैप्टर सारांश
  • Type! एक implicitly unwrapped optional declare करता है, हर access पर ! के बिना सीधे usable।
  • यह अब भी runtime पर crash करता है अगर nil रखते हुए access किया जाए, बिल्कुल एक forced unwrap की तरह।
  • यह मुख्य रूप से उन values के लिए उपयोग होता है जिनका declaration के तुरंत बाद set होना guaranteed है, पहले उपयोग से पहले।
  • ज़रूरत पड़ने पर इसे अब भी एक regular optional की तरह treat किया जा सकता है, जैसे if let के साथ।
🔒

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.