Showing posts with label NSDictionaries related.... Show all posts
Showing posts with label NSDictionaries related.... Show all posts

29 Nov 2017

How to add two NSDictionaries in Swift...

   The below code is an example of adding two dictionaries,
    
    var dict1 = ["One": "1","Two": "2","Three": "3","Four": "4"]
    let dict2 = ["Five": "5","Six": "6","Seven": "7","Eight": "8"]
    dict2.forEach { (k,v) in dict1[k] = v }
    print("Combined dictionary ===> ",dict1)
    
    And the Result is,
    

Result: Combined dictionary ===>  ["Two": "2", "Five": "5", "Three": "3", "Four": "4", "Six": "6", "One": "1", "Eight": "8", "Seven": "7"]

How to add two NSDictionaries into NSMutableDictionary Objective C...


The below code is an example of Adding Two Dictionaries into a Single mutableDictionary ,


  NSDictionary *dict1= [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"One",@"2",@"Two",@"3",@"Three",@"4",@"Four"nil];
    NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"5",@"Five",@"6",@"Six",@"7",@"Seven",@"8",@"Eight",nil];
    NSMutableDictionary *dictMerged  = [NSMutableDictionary dictionaryWithDictionary: dict1];
    [dictMerged addEntriesFromDictionary: dict2];
    
    NSLog(@"dictMerged===> %@",dictMerged);


/*
 Result: dictMerged===> {
Eight = 8;
Five = 5;
Four = 4;
One = 1;
Seven = 7;
Six = 6;
Three = 3;
Two = 2;
}
 */






Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0