Pheepster
Pheepster

Reputation: 6367

Building a dictionary at runtime in Swift 2.0

I am trying to build a simple dictionary at runtime in Swift. I am fairly new to Swift, but experienced in Obj-C (and missing it).

I am gathering some JSON data via a web service and looping through its elements. During this loop I need to build the dictionary. Here is the dictionary I need to generate

"gauge": {
    "gaugeID" : "03185"
    "name" : "SOME GAUGE NAME"
    "cfs" : 8410
    "stage" : 7.05
}

Since values for cfs and flow may not be present, I need to add these values to the dictionary conditionally.

I have declared the following dictionary

var dictEntry:[String:AnyObject]

Then as I loop through the dictionary I need to build each key-value and add it to the dictEntry dictionary. Every attempt I've made to do this fails. In Obj-C I could do the following:

[entryDict setValue:someValue forKey:@"cfs"];

How is this possible in Swift? Thanks!

Upvotes: 0

Views: 117

Answers (1)

TotoroTotoro
TotoroTotoro

Reputation: 17622

Here you go:

dictEntry["cfs"] = someValue

Upvotes: 2

Related Questions