How do I access an Array stored in a Dictionary in Swift?

466 views Asked by At

This is how I created my Dictionary with the Array:

@IBAction func didTapSaveButton(sender: UIButton) {
    var hooObject = Dictionary<String, [String]>()
    hooObject["\(dayLabel.text)"] = ["\(fromTimeLabel.text) - \(toTimeLabel.text)", "\(costField.text)"]
    NSLog("RESULT: \(hooObject)")
}

NSLog result:

 RESULT: [Optional("Wednesday"): [Optional("9:00 PM") - Optional("3:00 PM"), 40.00]]

I've tried this:

@IBAction func didTapSaveButton(sender: UIButton) {
    var hooObject = Dictionary<String, [String]>()
    hooObject["\(dayLabel.text)"] = ["\(fromTimeLabel.text) - \(toTimeLabel.text)", "\(costField.text)"]
    var res = hooObject["Wednesday"]
    NSLog("RESULT: \(res)")

}

This returns nil which doesn't make sense to me. Wednesday is the stored key so I would have thought it would be enough to return the array.

What I'm trying to do:

Basically I have a table with 7 sections and each represent a day. In each day I'm storing time slots with a cost for booking an activity in that slot.

When I use numberOfRowsInSection I will be able to use the Dictionary key to get the number of rows (time slots) for a particular day that will be shown in the tableView. This was the easiest way I could think to do it with NOSql.

I store the dictionaries in a column in parse.com and access that dictionary in my tableView.

Anyway I don't understand why I can't seem to access the array with the key. I could easily go and use an NSMutableDictionary but I'm using Swift for all my projects as a learning experience.

Wondering if it would be better to use a Dictionary with Dictionaries e.g. ["fromTime":value, "toTime":value, "cost":value]

Will appreciate any help given.

Thanks for your time.

2

There are 2 answers

1
Nate Cook On BEST ANSWER

The problem comes from using string interpolation on dayLabel.text -- since that's a String? and not a String, you end up with "Optional(Wednesday)" as your key instead of just "Wednesday". You need to unwrap it, like this:

@IBAction func didTapSaveButton(sender: UIButton) {
    var hooObject = Dictionary<String, [String]>()
    if let day = dayLabel.text {
        hooObject["\(day)"] = ["\(fromTimeLabel.text) - \(toTimeLabel.text)", "\(costField.text)"]
        NSLog("RESULT: \(hooObject)")
    }
}

You'll probably need to do something similar with the from and to time fields to get the values you want.

3
LondonGuy On

My answer:

@IBAction func didTapSaveButton(sender: UIButton) {
    var hooObject = Dictionary<String, [String]>()
    hooObject["\(dayLabel.text!)"] = ["\(fromTimeLabel.text) - \(toTimeLabel.text)", "\(costField.text)"]
    var res = hooObject["Wednesday"]
    NSLog("RESULT: \(res)")
}

I had to change dayLabel.text to dayLabel.text!

Those exclamation marks really baffle me. At first I thought they were like pointers because they can't be used on Int, Float etc (so I think).

But the fact I need one at the end of dayLabel.text and not my other labels baffles me even more. I guess in time I will have my ah hah moment. I did read a few posts on here but still don't quite get it.