3 Solutions Collect From Internet About “accessing nested dictionary from api in swift”

You need extra ? because data["floorplan_summary"]?["bedrooms"] returns AnyObject??. You have to unwrap it twice.

Why it returns AnyObject??? Because data["floorplan_summary"]? is AnyObject, and AnyObjectmay or may not have subscript. So, the first ? means, “If it has subscript“, and the second means “If subscript returns non nil“.

If you are wanting the syntax you described, I’d suggest using SwiftyJSON. It seems pretty popular, and it’s all implemented in a single swift file so not hard to add it to your project. It would look something like this.