0

I am having the following problem in Swift. First, I declare a data structure, like this:

var books = [String : Dictionary<String, Dictionary<String, Dictionary<String, Array<Dictionary<String, String>>>>>]()

I later initialize the var like this:

books = [
    "Fiction" : [
        "Genre Fiction" : [
            "Mystery" : [
                "Classics" : [
                    ["Title" : "Ten Little Indians",
                     "Author" : "Agatha Christie",
                     "read" : "no"],
                    ["Title" : "A Study in Scarlet",
                     "Author" : "Arthur Conan Doyle",
                     "read" : "no"],
                ]
            ]
        ]
    ]
]

Note that the compiler does not complain about it. Later, I create a new dictionary which I would like to append to that innermost array, like this:

var bookDict = Dictionary<String, String>()
bookDict = ["title" : dict.valueForKey("title") as! String,
            "author": dict.valueForKey("author") as! String,
            "read"  : dict.valueForKey("read") as! String ]
books["category"]["genre"]["focus"]["set"].append(bookDict)

However, I get a compiler error that "Cannot invoke append with an argument list of type (Dictionary < String, String>)". This is confusing to me because the books data structure is declared such that that innermost array is an array of Dictionary< String, String>.

What am I missing?

0

1 Answer 1

1

It's not clear during compile time if the key is really present in the dictionary so dictionaries always return optional values. You have to check this optionals:

if let category = books["Fiction"] {
    if let genre = category["Genre Fiction"] {
        if let focus = genre["Mystery"] {
            if var set = focus["Classics"] {
                set.append(bookDict)
            }
        }
    }
}

Or you can do it also like this:

books["Fiction"]?["Genre Fiction"]?["Mystery"]?["Classics"]?.append(bookDict)
Sign up to request clarification or add additional context in comments.

11 Comments

This gets me closer! It no longer gives a compiler error, which is good. I have modified the append call to pull the subscripted strings from another dictionary, like this:
bookage[dict.valueForKey("category") as! String]?[dict.valueForKey("genre") as! String]?[dict.valueForKey("focus") as! String]?[dict.valueForKey("set") as! String]?.append(bookDict)
So what is the problem now?
But when I later println() the dictionary all of the new entries are not there. Is it not the case that when a dictionary key is not found, that it creates the key automatically? Seems like it worked that way in Obj-C.
You have named the key "Fiction" not "category", "Genre Fiction" not "genre", etc... you are using the wrong keys according to the code you posted. Please post the full code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.