2

I am trying to create then append a dictionary but I am getting the following error I can't solve: Cannot subscript a value of type '[String: AnyObject]?' with an index of type 'String'

Anyone has an idea? Thanks in advance!

    var parameters: [String:[String:AnyObject]] = [
        "user": [
            "email": email,
            "password": passwordTextField.text,
            "first_name": firstName,
            "last_name": lastName
        ]
    ]

    parameters["user"]["status"] = "Connected"
vadian
  • 274,689
  • 30
  • 353
  • 361
johnson23
  • 286
  • 2
  • 16

2 Answers2

2

Whatever you retreive from a Dictionary may or may not exist, hence its return value is optional. if you are 100% certain, the returned value is not nil, use !, otherwise either check for nil or use ? (As pointed out by Lucian Boboc)

In your code, try replacing with the following.

parameters["users"]!["status"] = "Connected"
Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • 1
    That is because the return value is optional, if you are 100% the returned value is not nil, use "!", otherwise, use "?" – Lucian Boboc Aug 11 '15 at 07:55
2

parameters["user"] is an optional so you have to unwrap the optional explicitly, since you know the value exists.

parameters["user"]!["status"] = "Connected"
vadian
  • 274,689
  • 30
  • 353
  • 361