How to combine two NSDictionary in Swift
You can create a dictionary of tuples as follow:
let currencyname:[String:String] = ["CNY": "Chinese Yuan", "PLN": "Polish Zloty"]
let rawrates:[String:String] = ["CNY": "1.34", "PLN": "1.456"]
var combinedDictionary:[String:(name:String,rate:String)] = [:]
for key in currencyname.keys.array {
combinedDictionary[key] = (currencyname[key]!,rawrates[key]!)
}
// Testing
combinedDictionary["PLN"]! // (.0 "Polish Zloty", .1 "1.456")
combinedDictionary["PLN"]!.name // "Polish Zloty"
combinedDictionary["PLN"]!.rate // "1.456"
combinedDictionary["CNY"]! // (.0 "Chinese Yuan", .1 "1.34")
combinedDictionary["CNY"]!.name // "Chinese Yuan"
combinedDictionary["CNY"]!.rate // "1.34"
How to combine two NSDictionaries?
Did you init your NSMutableDictionary before adding entries to it?
NSMutableDictionary *configuration = [[NSMutableDictionary alloc] init];
How can I combine two Dictionary instances in Swift?
var d1 = ["a": "b"]
var d2 = ["c": "e"]
extension Dictionary {
mutating func merge(dict: [Key: Value]){
for (k, v) in dict {
updateValue(v, forKey: k)
}
}
}
d1.merge(d2)
Refer to the awesome Dollar & Cent project
https://github.com/ankurp/Cent/blob/master/Sources/Dictionary.swift
How to merge values with similar keys for NSdictionary
First make a struct(s) that represents the info you want in one cell:
struct AudioFileInfo {
let urlString: String
let text: String
init(dict: [String: String]) {
urlString = dict["AudioFileURL"] ?? ""
text = dict["AudioFileText"] ?? ""
}
}
struct CellInfo {
let date: String
let audioFileInfos: [AudioFileInfo]
}
Then you can:
let cellInfos = response
.flatMap {
$0.map { ($0.key, AudioFileInfo(dict: $0.value)) }
}
.reduce([CellInfo]()) { partial, item in
var new = partial
if let index = partial.index(where: { $0.date == item.0 }) {
new[index] = CellInfo(date: partial[index].date, audioFileInfos: partial[index].audioFileInfos + [item.1])
}
else {
new.append(CellInfo(date: item.0, audioFileInfos: [item.1]))
}
return new
}
This will give you an array of CellInfo objects.
As a general rule, this how you solve this sort of problem. First define a type that represents the output you want, then manipulate the input until you create objects of that type. Playground is your friend for this sort of thing.
Appending NSDictionary to other NSDictionary
You looking for this guy:
[NSMutableDictionary addEntriesFromDictionary:]
Make sure your UITableView
dictionary is an NSMutableDictionary
!
Check it here
How do you add a Dictionary of items into another Dictionary
You can define +=
operator for Dictionary
, e.g.,
func += <K, V> (left: inout [K:V], right: [K:V]) {
for (k, v) in right {
left[k] = v
}
}
Deep combine NSDictionaries
NSDictionary+Merge.h
#import <Foundation/Foundation.h>
@interface NSDictionary (Merge)
+ (NSDictionary *) dictionaryByMerging: (NSDictionary *) dict1 with: (NSDictionary *) dict2;
- (NSDictionary *) dictionaryByMergingWith: (NSDictionary *) dict;
@end
NSDictionary+Merge.m
#import "NSDictionary+Merge.h"
@implementation NSDictionary (Merge)
+ (NSDictionary *) dictionaryByMerging: (NSDictionary *) dict1 with: (NSDictionary *) dict2 {
NSMutableDictionary * result = [NSMutableDictionary dictionaryWithDictionary:dict1];
[dict2 enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
if (![dict1 objectForKey:key]) {
if ([obj isKindOfClass:[NSDictionary class]]) {
NSDictionary * newVal = [[dict1 objectForKey: key] dictionaryByMergingWith: (NSDictionary *) obj];
[result setObject: newVal forKey: key];
} else {
[result setObject: obj forKey: key];
}
}
}];
return (NSDictionary *) [[result mutableCopy] autorelease];
}
- (NSDictionary *) dictionaryByMergingWith: (NSDictionary *) dict {
return [[self class] dictionaryByMerging: self with: dict];
}
@end
Related Topics
Swift 4.2, String Firstindex() Function Error in Xcode Playground
Tapping an Mkmapview in Swiftui
How to Use Tabs to Evenly Space Out Description Strings in Swift
Searchbar Problem While Trying to Search Firestore and Reload the Tableview
Swift iOS 9: Section Header Change Position After Reload Data
Swift Associated Types and Protocol Inheritance
How to Scale/Position Nodes Swift Spritekit? Custom View
Swift 3 Issue with Cvararg Being Passed Multiple Times
Using Codable to Encode/Decode from Strings to Ints with a Function in Between
Pod Install Gpuimage Does Not Work
Error with Parse Query Findobjectsinbackgroundwithblock
If-Let Any to Rawrepresentable<String>
Split Paragraphs into Sentences
Drag a Cgrect Using Uipangesturerecognizer
How to Make Uicollectionview Reload Once It Receives Data from Firebase