इस लेख में, आप स्विफ्ट में वैकल्पिक, इसके उपयोग के मामलों और वैकल्पिक हैंडलिंग के बारे में जानेंगे।
पिछले लेख में, हमने स्विफ्ट में उपलब्ध विभिन्न डेटा प्रकारों के बारे में सीखा और उन प्रकारों के परिवर्तनीय या निरंतर घोषित किए गए डिफ़ॉल्ट मानों को भी देखा।
उदाहरण:
someValue = Int () प्रिंट करें (someValue)
जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:
०
हालाँकि स्विफ्ट में एक और डेटा प्रकार है जिसे वैकल्पिक कहा जाता है, जिसका डिफ़ॉल्ट मान एक शून्य मान ( nil
) है। जब आप एक चर या स्थिरांक चाहते हैं, तो आप वैकल्पिक का उपयोग कर सकते हैं। एक वैकल्पिक प्रकार में एक मान या अनुपस्थित मान (एक शून्य मान) हो सकता है।
गैर तकनीकी रूप से, आप एक जूता बॉक्स के रूप में वैकल्पिक सोच सकते हैं। जूता बॉक्स में जूता हो सकता है या नहीं। तो, आपको बॉक्स से जूता का उपयोग करते समय पहले से पता होना चाहिए।
वैकल्पिक कैसे घोषित करें?
आप एक डेटा प्रकार को वैकल्पिक रूप में जोड़कर !
या ?
करने के लिए प्रतिनिधित्व कर सकते हैं Type
। यदि किसी वैकल्पिक में एक मान होता है, तो वह मान लौटाता है Optional
, यदि वह नहीं लौटाता है nil
।
उदाहरण 1: स्विफ्ट में वैकल्पिक कैसे घोषित करें?
var someValue:Int? var someAnotherValue:Int! print(someValue) print(someAnotherValue)
जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:
नील नील
उपर्युक्त कार्यक्रम में, हमने एक वैकल्पिक प्रकार का उपयोग करके आरंभ किया है ?
और !
। वैकल्पिक बनाने के लिए दोनों तरीके मान्य हैं लेकिन एक बड़ा अंतर है जिसे हम नीचे बताएंगे।
एक वैकल्पिक इंट की घोषणा करने का मतलब है कि चर का पूर्णांक मान या कोई मूल्य नहीं होगा। चूंकि चर को कोई मान नहीं दिया गया है, आप स्क्रीन पर दोनों print
स्टेटमेंट आउटपुट देख सकते हैं nil
।
उदाहरण 2: एक वैकल्पिक से एक मान असाइन करना और एक्सेस करना
let someValue:Int? = 5 print(someValue) print(someValue!)
जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:
वैकल्पिक (5) 5
उपर्युक्त कार्यक्रम में, हमने एक वैकल्पिक Int
प्रकार और इसमें निर्दिष्ट मान 5 घोषित किया है।
आप देख सकते हैं, वैकल्पिक मुद्रण के रूप में print(someValue)
आप नहीं देता है 5
लेकिन Optional(5)
। यह जैसा कि ऊपर वर्णित फार्म की है: Optional
। इससे प्राप्त करने के लिए , हमें एक तंत्र की आवश्यकता होती है, जिसे अलिखित कहा जाता है ।
आप !
अगली पंक्ति में चर / स्थिरांक के अंत में अक्षर जोड़कर एक वैकल्पिक खोल सकते हैं print(someValue!)
। स्क्रीन पर print(someValue!)
वैकल्पिक और आउटपुट 5
को उजागर करता है ।
हालांकि, याद रखें, इस प्रकार के अलिखित तंत्र का उपयोग केवल तब किया जाना चाहिए जब आप निश्चित हों कि वैकल्पिक का निश्चित मूल्य होगा जब आप इसे एक्सेस करेंगे।
उदाहरण 3: स्पष्ट रूप से एक अलिखित वैकल्पिक घोषित करना
आप एक अलिखित वैकल्पिक भी बना सकते हैं:
let someValue:Int! = 5 print(someValue)
जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:
५
उपर्युक्त कार्यक्रम में, Int!
एक अलिखित वैकल्पिक बनाता है, जो स्वचालित रूप से मूल्य का उपयोग करता है, जबकि आप इसे एक्सेस करते हैं ताकि आपको हर बार !
चरित्र को जोड़ने की आवश्यकता न हो ।
जब आप इस प्रकार के वैकल्पिक का उपयोग करते हैं तो निश्चित रहें, जब आप इसे एक्सेस करते हैं तो चर को हमेशा एक मूल्य की आवश्यकता होगी। यदि आप नहीं करते हैं, तो आपको एक घातक त्रुटि दुर्घटना मिलेगी।
उदाहरण 4: एक अशक्त अलिखित वैकल्पिक का उपयोग करते समय घातक त्रुटि
var someValue:Int! var unwrappedValue:Int = someValue //crashes due to this line
जब आप प्रोग्राम चलाते हैं, तो आपको एक घातक त्रुटि के रूप में एक दुर्घटना मिलेगी : एक वैकल्पिक मूल्य को उजागर करते समय अप्रत्याशित रूप से शून्य पाया जाता है, क्योंकि कोड unwrappedValue:Int = someValue
वैकल्पिक someValue से चर unwrappedValue में मान असाइन करने का प्रयास करता है।
हालाँकि, somevalue एक Optional
प्रकार है जिसमें nil
मान होता है । वैरिएबल अनरैप्डवैल्यू को शून्य मान देने की कोशिश करना जो कि वैकल्पिक नहीं है, दुर्घटना को जन्म देगा।
इस मामले को संभालने के लिए विभिन्न तकनीकें हैं जिन्हें नीचे समझाया गया है।
वैकल्पिक हैंडलिंग
वैकल्पिक के मूल्य का उपयोग करने के लिए, इसे अलिखित करने की आवश्यकता है। वैकल्पिक मूल्य का उपयोग करने का बेहतर तरीका !
संचालक के उपयोग को बल देने के बजाय सशर्त अलिखित करना है।
ऐसा इसलिए होता है क्योंकि सशर्त रूप से अलंकृत करने वाले चेक से पूछते हैं कि क्या इस चर का कोई मूल्य है? । यदि हाँ, तो मूल्य दें, अन्यथा यह शून्य मामले को संभाल लेगा।
इसके विपरीत, बल प्रयोग करना कहते हैं कि जब आप इसका उपयोग करते हैं तो इस चर का कोई मूल्य नहीं होता है । इसलिए, जब आप एक वैरिएबल को खोलते हैं, जो शून्य होता है, तो आपका प्रोग्राम एक वैकल्पिक अपवाद और क्रैश को अनप्लग करते हुए अप्रत्याशित रूप से पाया गया निल फेंक देगा । सशर्त अलिखित के लिए कुछ तकनीकों को नीचे समझाया गया है:
1. अगर-कथन
You can use if statement and compare optional with nil to find out whether a optional contains a value or not. You can use the comparison operator "equal to" operator (==
) or the "not equal to" operator (!=
) in the if statement.
Example 5: Optional handling with if else statement
var someValue:Int? var someAnotherValue:Int! = 0 if someValue != nil ( print("It has some value (someValue!)") ) else ( print("doesn't contain value") ) if someAnotherValue != nil ( print("It has some value (someAnotherValue!)") ) else ( print("doesn't contain value") )
When you run the program, the output will be:
doesn't contain value It has some value 0
In the above program, the code inside if statement executes if an optional contain a value, otherwise the statement inside the else block executes. The major drawback of optional handling using this technique is, you still need to unwrap the value from optional using !
operator.
2. Optional Binding (if-let)
Optional binding helps you to find out whether an optional contains a value or not. If an optional contains a value, that value is available as a temporary constant or variable. Therefore, optional binding can be used with if statement to check for a value inside an optional, and to extract that value into a constant or variable in a single action.
Example 5: Optional handling using if let statement
var someValue:Int? var someAnotherValue:Int! = 0 if let temp = someValue ( print("It has some value (temp)") ) else ( print("doesn't contain value") ) if let temp = someAnotherValue ( print("It has some value (temp)") ) else ( print("doesn't contain value") )
When you run the program, the output will be:
doesn't contain value It has some value 0
In the above program, the code inside if statement executes if the optional contains a value. Otherwise the else block gets executed. The if-let
statement also automatically unwraps the value and places the unwrapped value in temp constant. This technique has major advantage because you don't need to forcely unwrap the value although being certain an optional contains a value.
3. Guard statement
You can use guard to handle optionals in Swift. Don't worry if you don't know what guard is. For now, just think of guard as an if-else
condition with no if block. If the condition fails, else statement is executed. If not, next statement is executed. See Swift guard for more details.
Example 6: Optional handling using guard-let
func testFunction() ( let someValue:Int? = 5 guard let temp = someValue else ( return ) print("It has some value (temp)") ) testFunction()
When you run the program, the output will be:
It has some value 5
In the above program, the guard contains a condition whether an optional someValue contains a value or not. If it contains a value then guard-let
statement automatically unwraps the value and places the unwrapped value in temp constant. Otherwise, else block gets executed and and it would return to the calling function. Since, the optional contains a value, print
function is called.
4. Nil-coalescing operator
In Swift, you can also use nil-coalescing operator to check whether a optional contains a value or not. It is defined as (a ?? b)
. It unwraps an optional a and returns it if it contains a value, or returns a default value b if a is nil.
Example 7: Optional handling using nil-coalescing operator
var someValue:Int! let defaultValue = 5 let unwrappedValue:Int = someValue ?? defaultValue print(unwrappedValue)
When you run the program, the output will be:
5
उपरोक्त कार्यक्रम में, वेरिएबल someValue को वैकल्पिक के रूप में परिभाषित किया गया है और इसमें शून्य मान होता है। नील coalescing ऑपरेटर वैकल्पिक को विफल करने में विफल रहता है इसलिए defaultValue देता है। इसलिए स्टेटमेंट print(unwrappedValue)
कंसोल में 5 आउटपुट करता है।
var someValue:Int? = 10 let defaultValue = 5 let unwrappedValue:Int = someValue ?? defaultValue print(unwrappedValue)
जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:
१०
हालांकि, उपरोक्त कार्यक्रम में, वैकल्पिक चर someValue को मूल्य 10. के साथ आरंभीकृत किया जाता है, इसलिए, निवल सहवर्ती संचालक कुछ वेल्यू से मूल्य को सफलतापूर्वक हटा देता है। इसलिए, स्टेटमेंट someValue ?? defaultValue
10 लौटाता है और स्टेटमेंट print(unwrappedValue)
कंसोल में 10 आउटपुट करता है।