6

I am having difficulty constructing this dictionary. My code looks like this:

var array: [String] = []
let params: [String: AnyObject] = [
    "presentation": [
            "array": array,
            "current_index": 0
    ]
]

The error shows up on the first line "presentation": [ with Contextual type 'AnyObject' cannot be used with dictionary literal. I have tried rewriting the array, initializing the params then setting the values, etc. etc. and I cannot figure out this problem. Any help would be awesome!

Thank you in advance!

Kiley
  • 409
  • 1
  • 5
  • 19
  • Depending on what you're trying to do with params, I believe you could also do: let params: [String: Any] This would give you more freedom as Any would cover both value and reference types. Of course it is always better to me more specific, as indicated in the accepted answer. – ProgrammierTier Sep 20 '16 at 13:47

2 Answers2

6

Try this in swift 3 it works

var array: [String] = []
let params: [String: AnyObject] = [
    "presentation": [
            "array": array,
            "current_index": 0
    ] as AnyObject
]
Arthi
  • 128
  • 1
  • 7
  • 4
    Please add some explanation to the code of what it does, this will also help future visitors. – kayess Nov 27 '16 at 11:33
  • 2
    The syntax is as put above in swift 3 - the var name will have data type String - with Anyobject, and the value of the var will also have an alias - as AnyObject in the end – Ashwin G Nov 27 '16 at 19:54
3

Try this

let params: [String: [String: AnyObject]]

And close the quotes after the current_index key.

Davyd Geyl
  • 4,578
  • 1
  • 28
  • 35
  • The quote error was just a typo when retyping the code into stackoverflow, but thank you that worked! – Kiley Sep 14 '16 at 17:45