1

I would like to know how I can make a key of a dictionary have multiple values according to the data that comes to it.

Attached basic example:

var temp = [String: String] ()

temp ["dinningRoom"] = "Table"
temp ["dinningRoom"] = "Chair"

In this case, I always return "Chair", the last one I add, and I need to return all the items that I am adding on the same key.

In this case, the "dinningRoom" key should have two items that are "Table" and "Chair".

4

3 Answers 3

4

You can use Swift Tuples for such scenarios.

//Define you tuple with some name and attribute type
typealias MutipleValue = (firstObject: String, secondObject: String)

var dictionary = [String: MutipleValue]()
dictionary["diningRoom"] = MutipleValue(firstObject: "Chair", secondObject: "Table")

var value = dictionary["diningRoom"]
value?.firstObject
4
  • And what if you have three values for the key?
    – JeremyP
    Apr 19, 2018 at 10:03
  • Add more values in upper tuple. Swift Tuple is for multiple return type. Apr 19, 2018 at 10:10
  • The point is, each tuple with a different count e.g. (T, U) and (T, U, V) is a different type in Swift. You can't store tuples with differing numbers of elements in the same dictionary.
    – JeremyP
    Apr 19, 2018 at 10:43
  • If your count raise to N. Then for this you need Arrays not tuples. Apr 19, 2018 at 11:02
3

You can declare a dictionary whose value is an array and this can contain the data you want, for example:

var temp = [String: [String]]()

temp["dinningRoom"] = ["Table", "Chair", "Bottle"]

If you want to add a new element you can do it this way:

if temp["dinningRoom"] != nil {
    temp["dinningRoom"]!.append("Flower")
} else {
    temp["dinningRoom"] = ["Flower"]
}

Now temp["dinningRoom"] contains ["Table", "Chair", "Bottle", "Flower"]

4
  • @Laffen except that he has only just tested temp["dinningRoom"] to make sure it is not nil.
    – JeremyP
    Apr 19, 2018 at 9:42
  • @Laffen well, it's fixed now, so you can remove your downvote :-)
    – JeremyP
    Apr 19, 2018 at 10:02
  • @Laffen Then whoever made the downvote can remove it.
    – JeremyP
    Apr 19, 2018 at 10:06
  • 1
    The proper Swift 4 solution is temp["dinningRoom", default: []].append("Flower"), compare for example stackoverflow.com/a/47739419.
    – Martin R
    Apr 19, 2018 at 11:11
2

Use Dictionary like this:

var temp = [String: Any]()

temp["dinningRoom"] = ["Table", "Chair"]

If you want to fetch all the elements from dinningRoom. You can use this:

let dinningRoomArray = temp["dinningRoom"] as? [String]

for room in dinningRoomArray{
    print(room)
}

It is not compiled code but I mean to say that we can use Any as value instead of String or array of String. When you cast it from Any to [String] using as? the app can handle the nil value.

2
  • My typing mistake: user var temp = [String : Any]() Apr 19, 2018 at 9:40
  • It's better to use a strong type for the values e.g. [String]
    – JeremyP
    Apr 19, 2018 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.