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"
0

2 Answers 2

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"
1
  • 1
    That is because the return value is optional, if you are 100% the returned value is not nil, use "!", otherwise, use "?" Commented Aug 11, 2015 at 7: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"

Not the answer you're looking for? Browse other questions tagged or ask your own question.