Is there another approach to a huge list of dictio

2019-09-12 06:59发布

问题:

I'm new to Swift 2 Language and I came from Python. I have this structure for a mobile app project:

var college = ["Bachelor of Science in Computer Science":
                ["First Year":
                  ["First Semester": [
                      "Course Code": [<values>],
                      "Course Description": [<values>],
                      "Course Units": [<values>]
                      ],  // End of First Semester
                   "Second Semester": [
                      "Course Code": [<values>],
                      "Course Description": [<values],
                      "Course Units": [<values>]
                      ]   // End of Second Semester
                  ],  // End of First Year
                 // Continue up to Fourth Year (Fifth Year if there's any)
                 // Continue to next Degree for the current college
              ]  // End of variable

Apparently, Xcode seems to be stuck in indexing forever and none of the answers around works for me. If I remove this particular variable, the app will load just fine. Is there another workaround for this?

Info that might help: The structure works fine in Python but it doesn't work with Swift when the data gets bigger. The nested dictionaries of lists works when the data structure is still small.

回答1:

You might be seeing Swift struggle to infer a type signature for this data structure. Specifying one can dramatically improve your compile/index times, e.g.

var college: [String: [String: [String: [String: [type_of_values]]]]] = ["Bachelor of Science in Computer Science":...

As is the compiler has to inspect all of those values to try to determine what the most specific available type common to all of them is.

You might also find it useful to define a set of domain models here so you're not just working with string keys. That would probably also resolve your compiler performance problem.



回答2:

You could always use a set of structs for your data and if you need to you can serialize and deserialize it into some other format such as JSON.

The advantages are speed, size, ease of programming (you get warnings for incorrect types and structure, plus autofilled parameters), readability, and flexibility.

struct Course {
  let code:String
  let description:String
  let units:String
}

enum Order {
  case First
  case Second
  case Third
  case Fourth
  case Fifth
  case Sixth
}

struct Semester {
  let order:Order
  let courses:[Course]
}

struct Year {
  let order:Order
  let semesters:[Semester]
}

struct Degree {
  let name:String
  let years:[Year]
}

let college = Degree(name: "Bachelor of Science in Computer Science",
  years: [
    Year(order: .First, semesters: [
      Semester(order: .First, courses: [
        Course(code: "CS101", description: "Some course.", units: "123"),
        Course(code: "CS102", description: "Some course.", units: "123")]),
      Semester(order: .Second, courses: [
        Course(code: "CS201", description: "Some course.", units: "123"),
        Course(code: "CS202", description: "Some course.", units: "123")])]),
    Year(order: .Second, semesters: [])]
)


标签: xcode swift