Currently I'm letting users import a CSV-file
created from Excel
or Numbers
. The sheet has multiple fields and I can import these without problem.
However, one of the fields looks like this:
"[Title1: (100: S11), (100: S12), (100: 10), (100: 2), (100: 3)], [Title2: (300: 50), (300: 90), (300: 100a), (300: D), (300: E)], [Title3: (500: 2), (500: 112), (500: 6), (500: 110), (500: 113)]"
How should I separate this field into a dictionary looking like this:
//[String: [[Int: String]]]
[
"Title1": [[100: "S11"], [100: "S12"], [100: "10"], [100: "2"], [100: "3"]],
"Title2": [[300: "50"], [300: "90"], [300: "100a"], [300: "D"], [300: "E"]],
"Title3": [[500: "2"], [500: "112"], [500: "6"], [500: "110"], [500: "113"]]
]
This field is created by the user itself and this already seems to be a bit too complicated and prone to error's. Perhaps a better question would be how to format this better instead of splitting this field into the dictionary I want?
Perhaps a simplified version:
//[String: [Int: [String]]]
[
"Title1": [100: ["S11", "S12", "10", "2", "3"]],
"Title2": [300: ["50", "90", "100a", "D", "E"]],
"Title3": [500: ["2", "112", "6", "110", "113"]]
]
Some explanation about this field:
Title: can be anything, it's the title of the nested dictionary
The key in the nested dictionary can be 100, 200, 300, 400, 500 or 600
The value in the nested dictionary is a String
which forms a unique key together with they key in this nested dictionary.
This means:
100: "S11" and 200: "S11" // valid
100: "S11" and 100: "S11" // not valid
extension String {
// Separate alphanumeric characters
func split(_ number: Int = 1) -> [String] {
let number = max(number, 1)
return self.components(separatedBy: CharacterSet.alphanumerics.inverted).filter { $0.characters.count > number - 1 }
}
}
let string = "Appetizer: (100: S11), (100: S12), (100: 10), (100: 2), (100: 3) * Main: (300: 50), (300: 90), (300: 100a), (300: D), (300: E) * Dessert: (500: 2), (500: 112), (500: 6), (500: 110), (500: 113)"
let values = string.components(separatedBy: "*").map { $0.split() }
var result: [String: [Int: [String]]] = [:]
values.forEach {
guard let key = $0.first else { return }
result[key] = result[key] ?? [:]
let values = $0.dropFirst()
let name = values.enumerated().filter { $0.0 % 2 == 0 }.map { $0.1 }
let code = values.enumerated().filter { $0.0 % 2 != 0 }.map { $0.1 }
zip(name, code).forEach {
guard let category = Int($0.0) else { return }
result[key]?[category] = (result[key]?[category] ?? []) + [$0.1]
}
}
This code produces the result I wanted, but it seems very ugly and it's too much dependant on the location of the values (instead of checking the values).
Any suggestion on how to do this using a much simpler and more elegant way?
(100: S11), (100: S12)
represent?NSManagedObject
since I'll have to save this in CoreData).(100: S12)
represents(Int: String)
where100
represents the category andS12
the identifier.CSV
file so I can also use the same format to export.NSRegularExpression
to achieve the result instead, so I can put the title in parentheses and items in brackets.