Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary in Swift

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"

like image 844
vichhai Avatar asked Dec 10 '22 23:12

vichhai


2 Answers

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.

like image 198
vacawama Avatar answered Dec 29 '22 10:12

vacawama


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.

like image 34
ScottyB Avatar answered Dec 29 '22 11:12

ScottyB