5

Currently, in Swift 3, there are 2 ways to write dictionary types. These are Dictionary<String, Any> and [String: Any]. I know that the former is compatible with Objective-C key/value coding and the latter is not but other than this are there any major differences between them or any performance implications I should be aware of?

I am currently swaying towards using the former for its Obj-C key/val compatibility but please warn me if this is a slippery slope.

I suppose the same could be said for Array<String> and [String] too?

Jacob King
  • 6,025
  • 4
  • 27
  • 45
  • 1
    The is no difference at all between `Dictionary` and `[String: Any]`, or between `Array` and `[String]`. – Martin R Jan 23 '17 at 08:53
  • http://stackoverflow.com/questions/29354511/syntax-to-create-dictionary-in-swift, http://stackoverflow.com/questions/34240602/difference-between-string-and-arraystring. – Martin R Jan 23 '17 at 09:15

2 Answers2

6

Dictionary<String, Any> and [String: Any], Array<String> and [String] are same. There is no difference at all.

Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
2

Dictionary and [String: Any] are different syntax for the same thing. If you want key-value coding you can use NSDictionary or NSArray.

var swiftarray: Array = []
// Fill the array with objects
var array: NSArray = (swiftarray as NSArray).valueForKeyPath("key.path") as NSArray
ptoinson
  • 2,013
  • 3
  • 21
  • 30