How to merge dictionaries in swift 5

Published November 30, 2021

 In this IOS Swift5 example code we cover Merge Dictionaries. Many of us will stuck in some point of view while merging two dictionaries in swift. Here is the simple example to merge swift dictionary with other dictionary.

 

import UIKit

class ViewController: UIViewController {

let dict1: [String: Any] = ["kFacebook": ["kToken": "token"]]
let dict2: [String: Any] = ["kRequest": ["kTargetUserId": "userId"]]
let dict3: [String: Any] = ["pRequest": ["kTargetUserId": "userId"]]

var combinedAttributes : NSMutableDictionary!

override func viewDidLoad() {
super.viewDidLoad()


combinedAttributes = NSMutableDictionary(dictionary: dict1)

combinedAttributes.addEntries(from: dict2)
combinedAttributes.addEntries(from: dict3)

print(combinedAttributes)
// Do any additional setup after loading the view.
}


}

As you seen above code (addEntries) acts as a adding multiple dictionaries into one dictionary

 

Swift5 Merge Dictionaries

 

 

Download Source code

 

 

Related Topics

Create First Ios project in Xocde 12

Create First SwiftUI project in Xocde13

Disable screen recording programatically

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1031 Views