स्विफ्ट शब्दकोश (उदाहरण के साथ)

इस ट्यूटोरियल में, आप डिक्शनरी क्या है, डिक्शनरी बनाकर और डिक्शनरी में कुछ सामान्य ऑपरेशन्स के बारे में जानेंगे।

पिछले स्विफ्ट एरेस लेख में, हमने सीखा कि हम एक चर / स्थिर में कई मूल्यों को कैसे संग्रहीत कर सकते हैं। इस लेख में, हम चर्चा करने जा रहे हैं कि हम डेटा / वैल्यूज़ को मुख्य मूल्य जोड़े के रूप में कैसे संग्रहीत कर सकते हैं।

एक शब्दकोश क्या है?

एक डिक्शनरी बस एक कंटेनर है जो अनियंत्रित तरीके से कई डेटा को की-वैल्यू पेयर के रूप में पकड़ सकता है।

प्रत्येक मान एक अद्वितीय कुंजी के साथ जुड़ा हुआ है और डेटा को बिना सेट की गई सूची में सेट के रूप में संग्रहीत करता है, अर्थात आपको उसी क्रम में तत्व नहीं मिलते हैं जैसे आपने शब्दकोश में आइटम को परिभाषित किया था।

जब आप संग्रह में कुछ पहचानकर्ता के साथ मूल्य देखने की आवश्यकता हो तो आप सरणी के बजाय शब्दकोश का उपयोग कर सकते हैं। मान लीजिए, आप देश की राजधानी को खोजना चाहते हैं। उस स्थिति में, आप प्रमुख देश और मूल्य राजधानी शहर के साथ एक शब्दकोश बनाएंगे। अब, आपको प्रमुख देश के साथ खोज कर संग्रह से राजधानी शहर मिलता है।

सरल शब्दों में, आप एक कुंजी को एक मूल्य में जोड़ते हैं। उपरोक्त उदाहरण में, हमने एक देश को उसकी राजधानी शहर में जोड़ा।

स्विफ्ट में एक शब्दकोश कैसे घोषित करें?

आप key:valueवर्ग कोष्ठक के अंदर डेटा प्रकार निर्दिष्ट करके एक खाली शब्दकोश बना सकते हैं ()

उदाहरण 1: एक खाली शब्दकोष की घोषणा करना

 let emptyDic:(Int:String) = (:) print(emptyDic) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 (:)

या

आप नीचे दिए गए खाली शब्द को भी परिभाषित कर सकते हैं:

 let emptyDic:Dictionary = (:) print(emptyDic) 

उपर्युक्त कार्यक्रम में, हमने टाइप की कुंजी और प्रकार के Intमूल्य के साथ टाइप डिक्शनरी की निरंतर रिक्ति घोषित की है Stringऔर इसे 0 मानों के साथ आरंभीकृत किया है।

या

चूंकि, स्विफ्ट एक प्रकार की इंजेक्शन भाषा है, आप डेटा प्रकार को निर्दिष्ट किए बिना सीधे शब्दकोश भी बना सकते हैं, लेकिन कुछ मूल्यों के साथ आरंभ करना चाहिए ताकि कंपाइलर इसके प्रकार का अनुमान लगा सके:

उदाहरण 2: कुछ मूल्यों के साथ एक शब्दकोश की घोषणा

 let someDic = ("a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9) print(someDic) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 ("बी": 2, "ए": 1, "आई": 9, "सी": 3, "ई": 5, "एफ": 6, "जी": 7, "डी": 4, " एच ": 8)

उपरोक्त कार्यक्रम में, हमने स्पष्ट रूप से परिभाषित किए बिना, लेकिन कुछ डिफ़ॉल्ट तत्वों के साथ आरंभ किए बिना एक शब्दकोश घोषित किया है।

तत्व कुंजी में है: मूल्य जोड़ी जहां कुंजी प्रकार की है Stringऔर मूल्य Intप्रकार का है। चूंकि डिक्शनरी एक अनियंत्रित सूची है print(someDic), परिभाषित किए गए की तुलना में भिन्न क्रम में मानों को आउटपुट करता है।

उदाहरण 3: दो सरणियों से शब्दकोश बनाना

हम सरणियों का उपयोग करके एक शब्दकोश भी बना सकते हैं।

 let customKeys = ("Facebook", "Google", "Amazon") let customValues = ("Mark", "Larry", "Jeff") let newDictionary = Dictionary(uniqueKeysWithValues: zip(customKeys,customValues)) print(newDictionary) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 ("अमेज़ॅन": "जेफ़", "Google": "लैरी", "फेसबुक": "मार्क")

उपरोक्त कार्यक्रम में, zip(customKeys,customValues)customKeys और customValues ​​से मूल्य का प्रतिनिधित्व करने वाले प्रत्येक तत्व के साथ टपल का एक नया अनुक्रम बनाता है। ज़िप कैसे काम करता है, इसके बारे में अधिक जानने के लिए, स्विट ज़िप पर जाएँ।

अब, हम इस अनुक्रम को Dictionary(uniqueKeysWithValues:)इनिशियलाइज़र में पास कर सकते हैं और एक नया शब्दकोश बना सकते हैं। इसलिए, print(newDictionary)दो सरणियों से तत्वों के साथ एक नया शब्दकोश आउटपुट।

स्विफ्ट में शब्दकोश तत्वों का उपयोग कैसे करें?

सरणियों के रूप में, आप सबस्क्रिप्ट सिंटैक्स का उपयोग करके किसी शब्दकोश के तत्वों को एक्सेस कर सकते हैं। आपको उस मान की कुंजी को शामिल करने की आवश्यकता है जिसे आप शब्दकोश के नाम के तुरंत बाद वर्ग कोष्ठक में पहुँचना चाहते हैं।

उदाहरण 4: एक शब्दकोश के तत्वों तक पहुँचना

 let someDic = ("a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9) print(someDic("a")) print(someDic("h")) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 वैकल्पिक (1) वैकल्पिक (8) 

आप फॉर-इन लूप का उपयोग करके किसी शब्दकोश के तत्वों का उपयोग भी कर सकते हैं।

उदाहरण 5: एक शब्दकोश के तत्वों तक पहुँचने के लिए लूप में

 let someDic = ("a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9) for (key,value) in someDic ( print("key:(key) value:(value)") ) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 कुंजी: बी मूल्य: २ कुंजी: एक मूल्य: १ कुंजी: मैं मूल्य: ९ कुंजी: सी मूल्य: ३ कुंजी: ई मूल्य: ५ कुंजी: एफ मूल्य: ६ कुंजी: जी मूल्य: 2 

स्विफ्ट में शब्दकोश तत्वों को कैसे संशोधित किया जाए?

आप सबस्क्रिप्ट सिंटैक्स का उपयोग करके शब्दकोश में तत्वों को जोड़ सकते हैं। आपको नई कुंजी को सबस्क्रिप्ट इंडेक्स के रूप में शामिल करना होगा और शब्दकोश के प्रकार का नया मान असाइन करना होगा।

उदाहरण 6: एक शब्दकोश में तत्वों की स्थापना

 var someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") someDictionary("Japan") = "Tokyo" print(someDictionary) 

जब आप प्रोग्राम चलाते हैं, तो आउटपुट होगा:

 ("Japan": "Tokyo", "China": "Beijing", "India": "NewDelhi", "Nepal": "Kathmandu")

In the above example, we've created a new key-value pair "Japan": "Tokyo" in the given dictionary by using the subscript syntax.

You can also use subscript syntax to change the value associated with a particular key as:

Example 7: Changing elements of a dictionary

 var someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") someDictionary("Nepal") = "KATHMANDU" print(someDictionary) 

When you run the program, the output will be:

 ("China": "Beijing", "India": "NewDelhi", "Nepal": "KATHMANDU")

Some helpful built-in Dictionary functions & properties

1. isEmpty

This property determines if an dictionary is empty or not. It returns true if a dictionary does not contain any value otherwise returns false.

Example 8: How isEmpty works?

 let someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") print(someDictionary.isEmpty) 

When you run the program, the output will be:

 false

2. first

This property is used to access the first element of a dictionary.

Example 9: How first works?

 let someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") print(someDictionary.first) 

When you run the program, the output will be:

 Optional((key: "China", value: "Beijing"))

3. count

This property returns the total number of elements (key-value pair) in a dictionary.

Example 10: How count works?

 let someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") print(someDictionary.count) 

When you run the program, the output will be:

 3

4. keys

This property returns all the keys inside the dictionary.

Example 11: How keys works?

 var someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") let dictKeys = Array(someDictionary.keys) print(dictKeys) 

When you run the program, the output will be:

 ("China", "India", "Nepal")

Similarly, you can use values to get all the values inside the dictionary.

5. removeValue

This function removes and returns the value specified with the key from the dictionary. Both key value pair will be removed from the dictionary.

Example 12: How removeValue() works?

 var someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") let val = someDictionary.removeValue(forKey: "Nepal") print(val) print(someDictionary) 

When you run the program, the output will be:

 Optional("Kathmandu") ("India": "NewDelhi", "China": "Beijing") 

Similarly, you can also use removeAll function to empty an dictionary.

Things to Remember

1. While using subscript syntax to access elements of an dictionary in Swift, you must be sure the key lies in the index otherwise you will get a nil value. Let's see this in example:

Example 13: Key must be present

 var someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") let val = someDictionary("Japan") print(val) 

When you run the program, the output will be:

 nil

In the above program, there is no key Japan. So when you try to access the value of the key "Japan", you will get a nil value.

2. Likewise, key-values are case-sensitive in Swift, so you must make sure the correct cased key/value is used. Otherwise, you will get a nil value. Let's see this in example:

Example 14: Keys are case-sensitive

 var someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") let val = someDictionary("nepal") print(val) 

When you run the program, the output will be:

 nil

In the above program, there is no key nepal. So when you try to access the value of the key "nepal", you will get a nil value.

3. There is also a way to provide a default value if the value for a given key does not exist. Let's see this in example:

Example 12: Default value for non-existent key

 var someDictionary = ("Nepal":"Kathmandu", "China":"Beijing", "India":"NewDelhi") let val = someDictionary("nepal", default:"Not Found") print(val) 

When you run the program, the output will be:

 Not Found

उपर्युक्त कार्यक्रम में, हमने डिक्शनरी का उपयोग करते हुए डिफ़ॉल्ट पैरामीटर में नहीं पाया गया मान निर्दिष्ट किया है । यदि मान कुंजी के लिए मौजूद नहीं है, तो डिफ़ॉल्ट मान वापस आ जाता है अन्यथा मान वापस कर दिया जाता है।

हमारे मामले में, कुंजी "नेपल" मौजूद नहीं है, इसलिए प्रोग्राम रिटर्न नहीं मिला

दिलचस्प लेख...