Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested dictionaries Swift curly braces?

I'm trying to create a nested dictionary in Swift, so I can change it to JSON later. I am setting it up like this:

let item: [String: AnyObject] = [
        "item": item,
        "price": price,
        "date": date,
        "time": time,
        "location": [
          "store": "Store name",
          "address": "1234 Untitled Dr.",
          "city": "City",
          "state": "CA",
          "zip": "12345"
        ]
      ]
print(item)

When I run this, the output is this:

["price": 15.69, "location": {
    address = "12350 Carmel MT Road";
    city = "City";
    state = CA;
    store = "Store name";
    zip = 12345;
}, "item": item, "date": , "time": ]

The location value is supposed to be a nested dictionary. Why are there curly braces surrounding the location part? And why are values inconsistent in whether there are quotes? Furthermore, when I try to access:

print(items["location"]!["store"])

I am told I:

Cannot subscript a value of type `[[String : AnyObject]]' with an index of type 'String'

like image 462
Timothy Deng Avatar asked Sep 07 '16 19:09

Timothy Deng


People also ask

What is a nested Dictionary?

Nested dictionary means a dictionary inside a dictionary. Therefore, it is a collection of all dictionaries into one dictionary. For example, we are going to create a dictionary of marks of students. marks = {'John':{'Math':90,'Physics':80,'Chemistry':75.5}, 'Susan':{'Math':80,'Physics':95,'Chemistry':60}} print(marks) Output:-

How to iterate over the elements of a dictionary in Swift?

We use the for loop to iterate over the elements of a dictionary. For example, We can use the count property to find the number of elements present in a dictionary. For example, In Swift, we can also create an empty dictionary. For example, In the above example, we have created an empty dictionary. Notice the expression

How to access keys and values of a dictionary in Swift?

Initially, the value associated with the key 112 is "Kyle". Now, notice the line, Here, we have changed the value associated with the key 112 to "Stan". In Swift, we can access the keys and values of a dictionary independently. 1. Access Keys Only We use the keys property to access all the keys from the dictionary. For Example,

What is swift Dictionary?

Swift dictionary is an unordered collection of items. It stores elements in key/value pairs. Here, keys are unique identifiers that are associated with each value.


1 Answers

Short answer:
Use [String: Any] instead of [String: AnyObject].

Longer answer::
The thing is Foundation automatically bridges Swift core types to Cocoa. This being said Swift Dictionary is being automatically bridged to NSDictionary because of the AnyObject constraint, a Swift struct can't be casted to an AnyObject and Dictionary is a struct.

The print function calls description property, which for NSDictionarys is with curly braces.

When specifying Any instead of AnyObject Swift will just set the variable type to Dictionary which has the square brackets description.

P.S.
About the subscript thing, I think you have an array there, so you should use something like items[0]["location"]!["store"].

like image 67
S2dent Avatar answered Sep 30 '22 15:09

S2dent