anto0522
anto0522

Reputation: 672

How to combine two NSDictionary in Swift

I am a beginner in swift and I am trying to apprehend the notion of dictionaries.

I have two NSDictionary that both contain the same keys, as follow:

var currencyname: NSDictionary = [
        "CNY": "Chinese Yuan",
        "PLN": "Polish Zloty"
]

var rawrates NSDictionary = [
        "CNY": "1.34",
        "PLN": "1.456"
]

I am trying to combine them so that I get only one Dictionary such as:

        ["CNY": "Chinese Yuan","1.34"]
        ["PLN": "Polish Zloty","1.456"]

I guess my first question is what sort of variable should I put the output in ? Can I use an NSDictionary ? From reading the documentation my understanding was that NSDictionaries work by pairs of Key/Values. Is it possible to put two values inside the dictionary ?

My Second question is how should I go about combining those two Dictionaries, I have tried to use the code below without much success

for (currency, rawrate) in rawrates {
                for (currencyid, name) in currencyname{
                    if currency == currencyid {
                        rawrates.append(name as String)
                    }
                } 
}

Upvotes: 4

Views: 2951

Answers (3)

Woodstock
Woodstock

Reputation: 22926

Another simple answer, using string interpolation and which is type safe:

var currencyName = ["CNY":"Chinese Yuan", "PLN": "Polish Zloty"]
var rawRates = ["CNY":"1.34" , "PLN":"1.456"]
var combined = [String:String]()

for (ccyCode, ccyName) in currencyName
{
    if let possibleRate = rawRates[ccyCode]
    {
        combined[ccyCode] = "\(ccyName), \(possibleRate)"
    }
    else
    {
        combined[ccyCode] = "\(ccyName), N/A"
    }     
}

Upvotes: 0

Leo Dabus
Leo Dabus

Reputation: 236360

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"

Upvotes: 2

nhgrif
nhgrif

Reputation: 62052

We can't quite combine dictionaries in this manner. The problem is, a dictionary only allows one value per key.

The solution is to instead have a dictionary which looks something like this:

[
    "CNY" : ["Chinese Yuan","1.34"],
    "PLN" : ["Polish Zloty","1.456"]
]

So our keys are "CNY" and "PLN", but each key has an array of values.

Here's one possible interpretation on combining your dictionaries:

var combinedDict = [String:Array<Any>]()
for key in currencyName.allKeys {
    combinedDict[key] = [currencyName[key], rawRates[key]]
}
println(combinedDict)

But honestly, what makes the most sense here is to probably simply make something to hold all of our currency information.

struct Currency {
    let name: String?
    let rawRate: String?
}

And now build a dictionary of these objects:

var currencyInformation = [String:Currency]()
for key in currencyName.allKeys {
    combinedDict[key] = Currency(name: currencyName[key], rawRate: rawRates[key])
}

Upvotes: 1

Related Questions