0

I want to know, how to put different keys and values into a dictionary, I see many codes that use the any, like this:

var dict = [String : Any]()

In that code, we need to have a String key and different types of values (bool, double, int, float). In my case I want to put different Types of values and keys, like that code below:

var dicionario0 = ["Ui": "Valor1", 5 : "ol", "Spt" : 78]

dicionario0[98] = 900

dicionario0[122] = "Valores"

dicionario0["Olas"] = 2333321

dicionario0[23.5] = 900

dicionario0[true] = 900

For that I try:

var dict = [Any:Any]()

But not work, I want Any for Keys and Any for Values, like the variable dicionario0, How I can do this?

LettersBa
  • 747
  • 1
  • 8
  • 27
  • 1
    Try var dict = [AnyObject:AnyObject](). And if you create var dict = ["a":3,4:"ab"] it will create dictionary of [NSObject: NsObject]. Try – Amit89 May 14 '15 at 14:42
  • 1
    @Amit89, `[AnyObject: AnyObject]` won't work because it's required the key type conforms to `Hashable` - `AnyObject` doesn't conform to `Hashable`. This is the same reason you can't use `Any` for the key. @LettersBa, I would recommend looking at http://stackoverflow.com/questions/24119624/how-to-create-dictionary-that-can-hold-anything-in-key-or-all-the-possible-type – ABakerSmith May 14 '15 at 14:45
  • 1
    @Amit89 AnyObject does not conform to Hashable and Equitable so it cant be used like a key – Zell B. May 14 '15 at 14:46
  • 1
    @ABakerSmith, Yes got it. Good link. – Amit89 May 14 '15 at 15:01
  • Great Using [NSObject:NSObject](), solve my problem, have a problem with this? I think not but NSObject is a super class father than others...so I think that using NSObject its not a problem... – LettersBa May 14 '15 at 15:02

0 Answers0