Dictionary in Swift.

Swift providing three type of Collection known as Array (Array in Swift) , Set and Dictionary to storing the value. Array store the value in the order. Set store the value unordered but unique. and Dictionary store the value in unordered  with key-value.

Dictionary stores the keys of same type and values of same type in the collection without the ordering. A value associated with its unique key which act as an identifier of the value in dictionary. items in the dictionary don't have specific order.

This example provide to definition and its useful function of Dictionary.

Create an empty Dictionary :
// Initialize the Dictionary
let dict1 = [String:String]()

// Initialize the Dictionary
let dict2 : [String:String] = [:]
Create default Dictionary :
// Default dictionary
let myDefaultDict : [String : String] = ["name" : "Kirit" , "surname" : "Modi"]
Get all keys in Dictionary:
let myAllKeys : [String : String] = ["name" : "Kirit" , "surname" : "Modi"]

// Get array of keys
let allKeys = Array(myAllKeys.keys)
Get all values in Dictionary:
let myValue : [String : String] = ["name" : "Kirit" , "surname" : "Modi"]

// Get array of Values
let allValues = Array(myValue.values)
Add item in Dictionary:
var myDictionary : [String : String] = ["name" : "Kirit" , "surname" : "Modi"]

// Adding new item with key and value.
myDictionary["lastName"] = "Di"
Check if  two Dictionary are equal:
let dictFirst = ["name" : "Kirit"]
let dictSecond = ["name" : "hitesh"]

// Checking two dictionary equality.
if dictFirst == dictSecond{
    print("Not Same")
Check object is Dictionary :
let mydictObject = ["firstName" : "Kirit"]

if mydictObject is Dictionary{
    print("This is dictionary object.")
Get  Dictionary value form key :
// Initialize the Dictionary
var dictPersonalDetail  = ["name" : "Kirit" , "surname" : "Modi"]

// Get value form Key
Size of dictionary items :
// Initialize the Dictionary
var dictSize  = ["name" : "Kirit" , "surname" : "Modi"]

// Size of Dictioanry
let size = dictSize.count
Loop gets keys and value from Dictionary :
// Initialize the Dictionary
var dictLoop  = ["name" : "Kirit" , "surname" : "Modi"]

//key and value inside loop.

for (key,value) in dictLoop {
Remove All items from Dictionary :
// Initialize the Dictionary
var dictRemove  = ["name" : "Kirit" , "surname" : "Modi"]

// Remove All element.
Remove item from Dictionary with key :
// Initialize the Dictionary
var dictKeyValueRemove  = ["name" : "Kirit" , "surname" : "Modi"]

// Remove "Name" key from Dictionary.
Set Dictionary element for key :
// Initialize the Dictionary
var dict = ["name": "John", "surname": "Doe"]

// Set the element with key: 'name' to 'Jane'
dict["name"] = "Jane"

Here I'm try to explain of Swift Dictionary and Its function , Please comment for more useful function which is missing in this tutorials

Another Collection type of Swift Array, See the tutorial of Swift Array. Array in Swift.


Dictionary in Swift. Dictionary in Swift. Reviewed by KIRIT MODI on 23:08:00 Rating: 5

No comments:

Powered by Blogger.