Access final deeply nested dictionary key pair

I have an array containing a deeply nested dictionary and would like to use the least amount of readable code possible to check for the final key pair:

let car: [String : [String : [String : String]]] = ["lunch" : ["fruit" : ["apple" : "red"]]]

I am looking into something similar to optional chaining (types) with filter and map as I couldn't get nielsbot's code to work, which looked promising, is there a simple way please?

Have you tried using the accepted answer from the Stack Overflow post that you linked?

To my shame, I couldn't actually understand it properly and I couldn't get it to work for what I needed as a result. I've never seen it anywhere before either.

let dictKeys = Array(editDict)
                
                for element in dictKeys {
                    if let result2 = element.value["lunch"]?["fruit"], let red = testA["apple"] {
                        print(testA)
                    }
                }

I had the following and was just fed up at the end of the day.

I think I figured out my problem.

Instead of copying out the plist data from an NSMutableDictionary into an array, which would only give me the top level keys, I realised that in order to change nested key values I would have to copy the contents to a Swift native dictionary and then jump through a number of hoops to change a specific value, then copy everything back!

The issue before was that while I could access a specific value with a native dictionary, I was unable to mirror the same ability with an NSMutableDictionay:

var nativeDictionary: [String : [String : [String : String]]] = ["level1" : ["level2" : ["keyWord" : "egg"]]]
nativeDictionary["level1"]?["level2"] = ["keyWord" : "bacon"]

The other issue I found was that if I attempted to amend the NSMutableDictionary nested value it would work, but replace the entire value set, which got me thinking about copying it aside, to work on it, and copy it back.

Checking "under the hood" to see what instance methods were available I realised that it was only possible to edit the top level key value pairs (and that cast into an Array the Any type wouldn't get me anywhere), and so it became obvious that I would need to extract the specific key value pairs and replace them:

muteDict //NSMutableDictionary
var muteEditedDict = Dictionary<String, Any>(_immutableCocoaDictionary: muteDict)  //convert NSMutableDictionary into a native Swift Dictionary: [String : Any]

var level1EditedDict = muteEditedDict["level1"] as? [String : [String : String]] //cast value one level in as nested dictionary
var level2EditedDict = level1EditedDict?["level2"] //cast value next level in as nested dictionary
level2EditedDict?.updateValue("newValue!", forKey: "keyWord") //edit the value to become: "newValue!"

level1EditedDict?["level2"] = level2EditedDict //copy the edited key pair back into the second level key pair
muteEditedDict["level1"] = level1EditedDict //copy the second level up into the first
editDict.setDictionary(muteEditedDict) //convert the chunk of plist back into the original NSMutableDictionary format expected

This was a lot more cumbersome than I had of wished for, and it is the only method I can get to reliably work, but I'm open to suggestions for anything else that's easier or more up to date (and safer?).

I wish that I could have just used muteDict["level1"]?["level2"] = ["keyWord" : "bacon"], but no, it had to be something a lot harder didn't it.