4

I really confuse with the way we create dictionary in swift. So could you please tell me what is the different between

var myDic3 = [String : AnyObject]()

and

var myDic2 = Dictionary <Int,AnyObject>()

and

var myDic4 = [ : ]

When i declare like myDic4 I cannot add key and value for it:

myDic4["001"] = "ABC"

And the error is "Cannot assign to the result of this expression"

vichhai
  • 2,548
  • 2
  • 15
  • 25

4 Answers4

8

In Swift, you can declare and initialize an empty Dictionary with type String for keys and type Any for values in 4 different ways:

  1. var myDic1 = [String : Any]()
  2. var myDic2 = Dictionary<String, Any>()
  3. var myDic3: [String : Any] = [:]
  4. var myDic4: Dictionary<String, Any> = [:]

These will all give you the same result which is an empty Dictionary with Strings as the keys and Anys as the values.

[String : Any] is just shorthand for Dictionary<String, Any>. They mean the same thing but the shorthand notation is preferred.

In cases 1 and 2 above, the types of the variables are inferred by Swift from the values being assigned to them. In cases 3 and 4 above, the types are explicitly assigned to the variables, and then they are initialized with an empty dictionary [:].

When you create a dictionary like this:

var myDic5 = [:]

Swift has nothing to go on, and it gives the error:

Empty collection literal requires an explicit type

Historical Note: In older versions of Swift, it inferred [:] to be of type NSDictionary. The problem was that NSDictionary is an immutable type (you can't change it). The mutable equivalent is an NSMutableDictionary, so these would work:

var myDic6: NSMutableDictionary = [:]

or

var myDic7 = NSMutableDictionary()

but you should prefer using cases 1 or 3 above since NSMutableDictionary isn't a Swift type but instead comes from the Foundation framework. In fact, the only reason you were ever able to do var myDic = [:] is because you had imported the Foundation framework (with import UIKit, import Cocoa, or import Foundation). Without importing Foundation, this was an error.

vacawama
  • 150,663
  • 30
  • 266
  • 294
3

Just to add to vacawama's answer If you would like to use different types for the dictionary keys you need to use [AnyHashable: Any]:

edit/update:

Swift 3 or later

var dict:[AnyHashable: Any] = [:]

dict[1] = "one"
dict["two"] = 2

print(dict)  // [AnyHashable("two"): 2, AnyHashable(1): "one"]
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
2

I believe the following declarations are all identical ways of declaring an empty Dictionary of type < String, Int > in Swift:

var dictionary1 = [String:Int]()
var dictionary2: [String:Int] = [:]
var dictionary3 = Dictionary<String, Int>()
var dictionary4: Dictionary<String, Int> = [:]

And these are identical ways of declaring an empty NSMutableDictionary in Swift:

var dictionaryA = [NSObject:AnyObject]()
var dictionaryB: [NSObject:AnyObject] = [:]
var dictionaryC = Dictionary<NSObject, AnyObject>()
var dictionaryD: Dictionary<NSObject, AnyObject> = [:]
var dictionaryE = NSMutableDictionary()
var dictionaryF: NSMutableDictionary = [:]

So many choices! I put both lists in my order of preference due to readability, but I would think "NSMutableDictionary()" would be prefered method if mixing with ObjectiveC or using with methods that take NSMutableDictionary parameters.

ScottyB
  • 2,167
  • 1
  • 30
  • 46
1
var myDic1 = Dictionary <String,AnyObject>()

is the "old" way of creating an empty dictionary with a String as a key type and AnyObject as a value type.

This one is the "modern" way and does the exact same thing:

var myDic2 = [String:AnyObject]()

But if you try to create one with this:

var myDic4 = [ : ]

you can't use it like a Swift dictionary because the array is not typed so Swift creates an NSDictionary instead.

But, you can use this notation to empty a Swift dictionary:

myDic1["001"] = "ABC"
println(myDic1["001"]) // Optional "ABC"
myDic1 = [:]
println(myDic1["001"]) // nil
myDic1["001"] = "ABC"
println(myDic1["001"]) // Optional "ABC"
Eric Aya
  • 69,473
  • 35
  • 181
  • 253