Implicitly Unwrapped Optionals क्या हैं
एक implicitly unwrapped optional एक value है जो technically एक optional box है, लेकिन Swift आपको इसे हर बार खोले बिना सीधे उपयोग करने देता है, यह भरोसा करते हुए कि setup के बाद इसके अंदर कुछ होगा।
Syntax
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
// 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())
Login to try C/C++/Java/PHP code in the editor
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
// 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")
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. #}
आम गलतियां
String!को regular optionals के general substitute के रूप में उपयोग करना; इसे उन cases के लिए reserved रखना चाहिए जहां एक value nil से शुरू होती है लेकिन उपयोग से पहले set होने की guarantee है।- यह भूल जाना कि एक implicitly unwrapped optional अब भी runtime पर crash करता है, बिल्कुल force-unwrapping की तरह, अगर इसे असल में
nilरखते हुए access किया जाए। - यह न समझना कि
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: