I am trying to parse some JSON structures onto a memory card using swift 2.0 - and I continue to run into this problem. The first time this happened, I thought it was some kind of strange thing that I did, but it happened again for the same reason, so I clearly don’t understand.
I created two unit tests that demonstrate. The first (testFails) assigns a value to the dictionary, and then creates an auxiliary value using the handle for the nested structure. The second (testWorks) always plays the full path from the top-level dictionary and builds it. The second version always works, but the first never works. The only thing I can think of is (1), the dictionary copies the link to the sub-map (subMap) or (2) something funky happens with the options.
It makes no sense to me either, and the debugger in Xcode sucks in so much that it never shows values for structures. The debugger says 0 keys / pairs, but when I print the count, I get the value, so this is useless. EG, put a breakpoint on the XCTAssertEqual line in testWorks, and the debugger sometimes says that the 'map' variable contains 0 key / value pairs, sometimes it just doesn't show anything - although there is clearly one key / value pair.
On the side (novice) note, I find that I resort to print statements in this very "modern" environment, because the Xcode debugger fails - it does not show me any data or something completely foreign. EG, in Java I could check the reference address of various variables and clarify when proxies / copies were made, but I cannot do this in Xcode.
func testFails()
{
var map = [String: [String: String]]();
var subMap = [String: String]();
map["map"] = subMap;
subMap["1"] = "2";
XCTAssertEqual(1, map.count);
XCTAssertEqual(1, map["map"]!.count);
XCTAssertEqual("2", map["map"]!["1"]);
}
func testWorks()
{
var map = [String: [String: String]]();
map["map"] = [String: String]();
map["map"]!["1"] = "2";
XCTAssertEqual(1, map["map"]!.count);
XCTAssertEqual("2", map["map"]!["1"]);
}