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.
The problem comes from using string interpolation on
dayLabel.text
-- since that's aString?
and not aString
, you end up with "Optional(Wednesday)" as your key instead of just "Wednesday". You need to unwrap it, like this:You'll probably need to do something similar with the from and to time fields to get the values you want.