Search code examples
swiftswift2trie

Swift deep NSObjects do not compile


In Swift, I am trying to implement a word Trie, using a literal representation as a series of nested NSObjects. Here is the Trie.

let GEENITRIE:NSObject = [
    "i":[
            "need":[
                    "tutoring":[
                            "in":[
                                    "**ARG**":"{courserequest}"]],
                    "a":[
                            "tutoring":[
                                    "session":[
                                            "in":[
                                                    "**ARG**":"{courserequest}"]]]]],
            "want":[
                    "tutoring":[
                            "in":[
                                    "**ARG**":"{courserequest}"]]],
            "would":[
                    "like":[
                            "tutoring":[
                                    "in":[
                                            "**ARG**":"{courserequest}"]]]]],
    "tutoring":[
            "in":[
                    "**ARG**":"{coureserequest}",
                    "and":[
                            "**ARG**":"{doublecourse}"]],
            "at":[
                    "**ARG**":"{timeparse}"]],
    "a":[
            "tutoring":[
                    "session":[
                            "in":[
                                    "**ARG**":"{courserequest}"],
                            "at":[
                                    "**ARG**":"{timeparse}"]]]]]

However, when I compile this with the rest of my command line app, it takes about 26 seconds, then the following error comes up: Error:(23, 26) expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

Why does this happen, and what is the best way to implement this so this does not happen?


Solution

  • It happens because the Swift compiler has to figure out the real type of that expression, and figuring it out might take too long.

    You should move your data into a file in JSON format:

     {
      "i":{
           "need":{
                   "tutoring":{
                               "in":{
                                     "**ARG**":"{courserequest}"}},
                   "a":{
                        "tutoring":{
                                    "session":{
                                               "in":{
                                                     "**ARG**":"{courserequest}"}}}}},
           "want":{
                   "tutoring":{
                               "in":{
                                     "**ARG**":"{courserequest}"}}},
           "would":{
                    "like":{
                            "tutoring":{
                                        "in":{
                                              "**ARG**":"{courserequest}"}}}}},
      "tutoring":{
                  "in":{
                        "**ARG**":"{coureserequest}",
                        "and":{
                               "**ARG**":"{doublecourse}"}},
                  "at":{
                        "**ARG**":"{timeparse}"}},
      "a":{
           "tutoring":{
                       "session":{
                                  "in":{
                                        "**ARG**":"{courserequest}"},
                                  "at":{
                                        "**ARG**":"{timeparse}"}}}}}
    

    Then load it at runtime:

    let jsonUrl = NSBundle.mainBundle().URLForResource("data", withExtension: "json")!
    let jsonData = NSData(contentsOfURL: jsonUrl)!
    let tree = try! NSJSONSerialization.JSONObjectWithData(jsonData, options: [])