0

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.

LondonGuy
  • 10,778
  • 11
  • 79
  • 151
  • 1
    Hard to say without seeing the full class, but it looks like you're populating the `hooObject` dictionary inside your event handler. If so, it will disappear at the end of that method, so accessing the data later won't be possible. You want it to be an instance property of your class. – Nate Cook Sep 16 '14 at 23:39
  • Yep just manually entered the strings in the Dictionary instead of grabbing the text from the label and textfield and when I call hooObject["Wednesday"] it shows me the array. Will try out your suggestion. – LondonGuy Sep 16 '14 at 23:46
  • @NateCook I made hooObject an instance var but no difference. The text in the label text and field text is still there after I tap save and I'm not dismissing the view or anything so would have thought I'd be able to access them. Makes no sense to me. I think I misunderstood what you said could be going on. – LondonGuy Sep 16 '14 at 23:55
  • This line: var res = hooObject["Wednesday"] NSLog("RESULT: \(res)") is actually called inside the method above so the values should still be accessible. I've edited my question. – LondonGuy Sep 16 '14 at 23:59
  • @NateCook I tried again but this time replacing "\(dayLabel.text)" with "Wednesday" and it works. So "\(dayLabel.text)" with the string Wednesday stored inside it isn't the same as "Wednesday". hmm – LondonGuy Sep 17 '14 at 00:03

2 Answers2

1

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.

Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • Figured this out just now too. I really don't understand why I can get away with not doing this on my other labels though. – LondonGuy Sep 17 '14 at 00:10
0

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.

LondonGuy
  • 10,778
  • 11
  • 79
  • 151
  • Yikes, don't do that - if dayLabel.text isn't set your app will crash on the spot. – Nate Cook Sep 17 '14 at 00:12
  • I always do form validation before submitting my forms. – LondonGuy Sep 17 '14 at 00:31
  • It's dangerous to use `!` to get past a compiler issue that you're not sure of - you're trading a compiler problem for a runtime problem, erasing a big safety feature of Swift. Read [this section on Optionals](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_483) to better understand what's going on. – Nate Cook Sep 17 '14 at 00:45